{"id":284769,"date":"2017-04-11T11:05:11","date_gmt":"2017-04-11T07:05:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=284769"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=284769","title":{"rendered":"Cluster + EXPRESS + socket.io \u0431\u0435\u0437 REDIS"},"content":{"rendered":"<p>\u041c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 soket.io. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u043e realtime \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041e\u043d\u0430 \u0441\u0430\u043c\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430, \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 WebSoket.<br \/>  \u042d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e Node.js \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 Cluset.<br \/>  \u041f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0432\u0441\u0442\u0430\u043b \u0432\u043e\u043f\u0440\u043e\u0441, \u0430 \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u043d\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e REDIS? \u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c EXPRESS.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 (\u043f\u043e\u043a\u0430 \u0431\u0435\u0437 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430): <a href=\"https:\/\/socket.io\/docs\/\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>  <\/p>\n<pre><code class=\"javascript\">var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server);  server.listen(3038);  app.get('\/', function (req, res) {   res.sendfile(__dirname + '\/index.html'); });  io.on('connection', function (socket) {   socket.emit('news', { hello: 'world' });   socket.on('my other event', function (data) {     console.log(data);   }); }); <\/code><\/pre>\n<p>  \u041f\u043e\u043a\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e, \u0432\u0441\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0432\u0435\u0448\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u043f\u043e\u0440\u0442 3038 \u0441 express \u0438 soket.io. \u041a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c:  <\/p>\n<pre><code>&lt;html&gt; &lt;head&gt; \t&lt;title&gt;Test socket.io&lt;\/title&gt; \t&lt;script src=&quot;socket.io.js&quot;&gt;&lt;\/script&gt; \t&lt;script&gt; \tvar connect_error_count = 0; \t \t \tvar socket = io.connect('http:\/\/localhost:3038\/', \t\t{ \t\t\t'reconnectionDelay': 10 \/\/ defaults to 500 \t\t} \t);  \t  \tsocket.on('connect_error', function(){ \t   \tconsole.log('Connection Failed'); \t    \/\/\u0415\u0441\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 5 \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u043f\u0435\u0440\u0435\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \t    connect_error_count++; \t    if (connect_error_count&gt;=5){ \t    \tsocket.disconnect(); \t    \tconsole.log(&quot;stop reconection&quot;); \t    } \t}); \t \t \t \tsocket.on('reconnect', function(){ \t    console.log('reconnect'); \t\tconnect_error_count=0; \t}); \t \t \tsocket.on('news', function (data) { \t  console.log(data); \t}); \t \t \t&lt;\/script&gt; &lt;\/head&gt; &lt;body&gt;  &lt;\/body&gt; &lt;\/html&gt;  <\/code><\/pre>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0439 \u043f\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u043c\u0443 \u043a\u043e\u0434\u0443. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 soket \u0441 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 (\u043f\u043e\u0440\u0442 3038, \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f 10\u043c\u0441). \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u0435\u0435 5 \u0440\u0430\u0437, \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 soket, \u0435\u0441\u043b\u0438 \u043c\u0435\u043d\u0435\u0435, \u0442\u043e \u043e\u0431\u043d\u0443\u043b\u044f\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a \u043a\u043e\u043c\u043d\u0430\u0442\u0435 news.<br \/>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u044d\u0442\u0430\u043f\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0430. \u0422.\u043a. \u0441\u043e\u043a\u0435\u0442 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0440\u0442\u0443, \u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u043e\u0432\u044b\u0439 worker \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c soket.io \u043d\u0430 \u043d\u043e\u0432\u043e\u043c \u043f\u043e\u0440\u0442\u0443:  <\/p>\n<pre><code class=\"javascript\">var cluster = require('cluster');\/\/\u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c cluster var cpuCount = require('os').cpus().length;\/\/\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044f\u0434\u0435\u0440 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 var io = []; \/\/\u0412 \u043c\u0430\u0441\u0442\u0435\u0440\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c worker'\u043e\u0432 \u0440\u0430\u0432\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u044f\u0434\u0435\u0440 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u0432 if (cluster.isMaster) {     for (var i = 0; i &lt; cpuCount; i += 1) {     \tvar worker = cluster.fork();     } }  \/\/\u0412 \u0432\u043e\u0440\u043a\u0435\u0440\u0435 if (cluster.isWorker) {                 var worker_id = cluster.worker.id; \t\tvar express  = require('express'); var app = express(); \t\tvar server = require('http').Server(app); \t\tio[worker_id] = require('socket.io')(server); \t\tserver.listen(3030+worker_id);   \t\tio[worker_id].on('connection', function (socket) { \t\t\tconsole.log( socket.id ); \t\t\tconsole.log( &quot;WORKER ID :&quot;+worker_id ); \t\t\tsocket.emit('news', { hello: 'world' }); \t\t\tsocket.on('my other event', function (data) { \t\t\tconsole.log(data); \t\t\t}); \t\t});  var app_express = express(); \t\tapp_express.listen(8081); \t\tapp_express.use(express.static('public'));\/\/\u043e\u0442\u0434\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u0441\u043c. \u043a\u043e\u0434 \u043a\u043b\u0438\u0435\u043d\u0442\u0430) \t\tapp_express.get('\/', function (request, response) { \t\t\tresponse.send('Hello from Worker '+worker_id);    \t\t\tconsole.log( '------' ); \t\t\t \t\t}); \t\t \t\t \t\tapp_express.get('\/get_port', function (request, response) { \t\t\tresponse.send(3030+worker_id);    \t\t\tconsole.log( 'get_port' ); \t\t}); }  \t\tapp_express.get('\/api', function (request, response) { \t\t\tresponse.setHeader('Content-Type', 'application\/json'); \t\t\tvar id = request.param('id'); \t\t\tvar msg = request.param('msg');                         var port = request.param('port'); \t\t\tvar JSON_DATA = { \t\t\t\t\t&quot;worker_id&quot;:worker_id \t\t\t\t\t,&quot;id&quot;:id \t\t\t\t\t,&quot;msg&quot;:msg                                         ,&quot;port&quot;:port \t\t\t}; \t\t}); <\/code><\/pre>\n<p>  \u0421\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e \u043f\u043e\u043a\u0430\u0436\u0435\u043c \u043a\u0430\u043a \u0432\u0441\u0435 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442:<br \/>  <img decoding=\"async\" src=\"https:\/\/pp.userapi.com\/c626531\/v626531214\/5bfa2\/P3Uu6qhTkvw.jpg\" alt=\"image\"\/><br \/>  \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 localhost:8081 cluster \u0441\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u0441 \u043d\u0430 worker \u043f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u044b\u0437\u043e\u0432 :<\/p>\n<blockquote><p>app_express.get(&#8216;\/api&#8217;, function (request, response){&#8230;}<\/p><\/blockquote>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043f\u0440\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 localhost:8081\/api?msg=test&#038;port=3032&#038;id=100500 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e \u043f\u043e\u0440\u0442\u0443 3032 id=100500 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 msg=teest.<br \/>  \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u044c soket.io \u043f\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u0440\u0442\u0443  <\/p>\n<pre><code class=\"javascript\">var http = require('http'); var client = http.createClient(3032 , &quot;localhost&quot;); request = client.request(); request.on('response', function( res ) {     res.on('data', function( data ) {         console.log( data );     } ); } ); request.end(); <\/code><\/pre>\n<p>  \u041d\u043e \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c broadcast \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u0438 \u043d\u0430\u043c \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u043f\u043e\u0440\u0442 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c, \u043c\u044b \u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u043d\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u0447\u0442\u043e \u043f\u0440\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0438 localhost:8081\/api?msg=test&#038;port=3032&#038;id=100500 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 worker 2.<br \/>  <img decoding=\"async\" src=\"https:\/\/pp.userapi.com\/c626531\/v626531214\/5bfb5\/H8g5ukCYbbI.jpg\" alt=\"image\"\/><br \/>  \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0447\u0442\u043e \u043f\u0440\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 broadcast \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0441 worker1 \u043c\u044b \u043d\u0435 \u0438\u043c\u0435\u0435\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u044f\u043c\u0443\u044e \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c soket.io 2, soket.io 3, soket.io 4, soket.io\u2026<br \/>  \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0432 cluster. \u0412 cluster&#8217;e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 master-&gt;worker \u0438 \u0438\u0437 worker-&gt;master. \u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0437\u0438\u043c \u044d\u0442\u043e \u0441\u0445\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e.<br \/>  <img decoding=\"async\" src=\"https:\/\/pp.userapi.com\/c626531\/v626531214\/5bfbe\/5Fy2jH63DhY.jpg\" alt=\"image\"\/><br \/>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u043e\u0439 master-&gt;worker \u0438 worker-&gt;master \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043d\u0438\u0436\u0435  <\/p>\n<pre><code class=\"javascript\">\/\/ \u041a\u043b\u0430\u0441\u0442\u0435\u0440\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 var cluster = require('cluster'); var io = []; var cpuCount = require('os').cpus().length; var workers = [];  if (cluster.isMaster) {     \/\/ Count the machine's CPUs     \/\/ Create a worker for each CPU     for (var i = 0; i &lt; cpuCount; i += 1) {     \tvar worker = cluster.fork();     \t     \t\/\/\u0421\u043b\u0443\u0448\u0430\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0442 workera         worker.on('message', function(data) {         \t\/\/\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u0435\u043c worker'\u0430\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435         \tfor (var j in workers) {workers[j].send(data);}         });         \/\/\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 worker \u0432 \u043c\u0430\u0441\u0441\u0438\u0432         workers.push(worker);     }           }    if (cluster.isWorker) { \t\t\/\/------------------------------------------------------------------------------------\/\/ \t\tvar worker_id = cluster.worker.id; \t\tvar express  = require('express'); \t\tvar app = express(); \t\tvar server = require('http').Server(app); \t\tio[worker_id] = require('socket.io')(server); \t\tserver.listen(3030+worker_id); \t\t \t\tio[worker_id].on('connection', function (socket) { \t\t\tconsole.log( socket.id ); \t\t\tconsole.log( &quot;WORKER ID :&quot;+worker_id ); \t\t\tsocket.emit('news', { hello: 'world' }); \t\t\tsocket.on('my other event', function (data) { \t\t\tconsole.log(data); \t\t\t}); \t\t}); \t\t\/\/------------------------------------------------------------------------------------\/\/ \t\tvar app_express = express(); \t\tapp_express.listen(8081); \t\tapp_express.use(express.static('public'));\/\/\u043e\u0442\u0434\u0430\u0435\u043c \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \t\tapp_express.get('\/', function (request, response) { \t\t\tresponse.send('Hello from Worker '+worker_id);    \t\t\tconsole.log( '------' ); \t\t\t \t\t}); \t\t \t\t \t\tapp_express.get('\/get_port', function (request, response) { \t\t\tresponse.send(3030+worker_id);    \t\t\tconsole.log( 'get_port' ); \t\t}); \t\t \t\t \t\tapp_express.get('\/api', function (request, response) { \t\t\t\/\/process.send(&quot;----------------------&quot;); \t\t\tresponse.setHeader('Content-Type', 'application\/json'); \t\t\tvar id = request.param('id'); \t\t\tvar msg = request.param('msg'); \t\t\tvar JSON_DATA = { \t\t\t\t\t&quot;worker_id&quot;:worker_id \t\t\t\t\t,&quot;id&quot;:id \t\t\t\t\t,&quot;msg&quot;:msg \t\t\t}; \t\t\t \t\t\tio[port-3030].to(msg.id).emit('news', msg.msg);  \t\t\t \t\t\tresponse.send(\tJSON.stringify(JSON_DATA) );   \t\t\t \t\t\t\/\/\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \t\t\tprocess.send(JSON_DATA); \t\t\t \t\t}); \t\t  \t\t\/\/\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043e\u0442 worker\/\/ \t\tprocess.on('message', function(msg){ \t\t\tconsole.log(worker_id); \t\t    console.log(msg.id); \t\t    console.log(msg.msg); \t\t    io[worker_id].to(msg.id).emit('news', msg.msg); \t\t     \t\t}); \t\t } <\/code><\/pre>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 cluster \u0438 \u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c \u043c\u0435\u0436\u0434\u0443 master-&gt;worker \u0438 worker-&gt;master. \u041a\u0440\u0430\u0442\u043a\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d soket.io. \u0418 \u0441\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f redis.<br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/326172\/\"> https:\/\/habrahabr.ru\/post\/326172\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 soket.io. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u043e realtime \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041e\u043d\u0430 \u0441\u0430\u043c\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430, \u0435\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 WebSoket.<br \/>  \u042d\u0442\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e Node.js \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 Cluset.<br \/>  \u041f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0432\u0441\u0442\u0430\u043b \u0432\u043e\u043f\u0440\u043e\u0441, \u0430 \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u043d\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e REDIS? \u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c EXPRESS.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-284769","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284769"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/284769\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}