Created
April 3, 2014 20:13
-
-
Save geekforbrains/9962001 to your computer and use it in GitHub Desktop.
ZeroMQ + Node.js Messaging Queue
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
var os = require('os'); | |
var cluster = require('cluster'); | |
var zmq = require('zmq'); | |
if(cluster.isMaster) { | |
var cpuCount = os.cpus().length; | |
for(var i = 0; i < cpuCount; i++) cluster.fork(); | |
} else { | |
var socket = zmq.socket('pull') | |
socket.identity = 'consumer' + process.pid; | |
socket.on('message', function(msg) { | |
console.log(socket.identity + ' received: ' + msg.toString()); | |
}); | |
socket.connect('tcp://0.0.0.0:9999'); | |
console.log(socket.identity + 'connected on 9999'); | |
} |
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
var zmq = require('zmq'); | |
var inflow = zmq.socket('pull'); | |
var outflow = zmq.socket('push'); | |
inflow.bind('tcp://0.0.0.0:9998', function(err) { | |
if(err) throw err; | |
console.log('inflow listening on 9998'); | |
inflow.on('message', function(msg){ | |
console.log('relaying: ' + msg); | |
outflow.send(msg); | |
}); | |
}); | |
outflow.bind('tcp://0.0.0.0:9999', function(err) { | |
if(err) throw err; | |
console.log('outflow listening on 9999'); | |
}); |
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
var zmq = require('zmq'); | |
var socket = zmq.socket('push'); | |
var counter = 0; | |
socket.connect('tcp://0.0.0.0:9998'); // connect to device.inflow | |
console.log('producer connected on 9998'); | |
setInterval(function(){ | |
console.log('sending %d', counter++); | |
socket.send(counter); | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment