Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because cluster.fork is implemented on top of <a href="http://nodejs.org/docs/v0.6.5/api/all.html#child_process.fork">child_process.fork</a>, you can send messages from a master to the worker by using <code>worker.send({ msg: 'test' })</code>, and from a worker to a master by <code>process.send({ msg: 'test' });</code>. You receive the messages like so: <code>worker.on('message', callback)</code> (from worker to master) and <code>process.on('message', callback);</code> (from master to worker).</p> <p>Here's my full example, you can test it by browsing <a href="http://localhost:8000/">http://localhost:8000/</a> Then the worker will send a message to the master and the master will reply:</p> <pre><code>var cluster = require('cluster'); var http = require('http'); var numReqs = 0; var worker; if (cluster.isMaster) { // Fork workers. for (var i = 0; i &lt; 2; i++) { worker = cluster.fork(); worker.on('message', function(msg) { // we only want to intercept messages that have a chat property if (msg.chat) { console.log('Worker to master: ', msg.chat); worker.send({ chat: 'Ok worker, Master got the message! Over and out!' }); } }); } } else { process.on('message', function(msg) { // we only want to intercept messages that have a chat property if (msg.chat) { console.log('Master to worker: ', msg.chat); } }); // Worker processes have a http server. http.Server(function(req, res) { res.writeHead(200); res.end("hello world\n"); // Send message to master process process.send({ chat: 'Hey master, I got a new request!' }); }).listen(8000); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload