Skip to content

Instantly share code, notes, and snippets.

@fanatid
Last active December 12, 2017 20:21
Show Gist options
  • Save fanatid/c8581ce7dd36761cb51120b383cb513e to your computer and use it in GitHub Desktop.
Save fanatid/c8581ce7dd36761cb51120b383cb513e to your computer and use it in GitHub Desktop.
node.js cluster: pipe
// 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