Last active
December 12, 2017 20:21
-
-
Save fanatid/c8581ce7dd36761cb51120b383cb513e to your computer and use it in GitHub Desktop.
node.js cluster: pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on: https://github.com/nodejs/node-v0.x-archive/issues/5727 | |
const cluster = require('cluster') | |
const net = require('net') | |
function runMaster () { | |
cluster.setupMaster({ stdio: [0, 1, 2, 'ipc', 'pipe'] }) | |
const worker = cluster.fork() | |
const pipe = worker.process.stdio[4] | |
pipe.on('data', (buffer) => { | |
console.log('master data:', buffer.toString()) | |
}) | |
pipe.write(Buffer.from('hello')) | |
} | |
function runWorker () { | |
const pipe = new net.Socket({ fd: 4 }) | |
pipe.on('data', (buffer) => { | |
console.log('worker data:', buffer.toString()) | |
pipe.write('world!') | |
}) | |
} | |
;(cluster.isMaster ? runMaster : runWorker)() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment