-
-
Save reqshark/38bb0d1c301025374d98 to your computer and use it in GitHub Desktop.
This file contains hidden or 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') | |
, xpub // xpub to redistribute messages over known port | |
, xsub; // xsub to recieve incomming messaes | |
var xpub_url = 'tcp://0.0.0.0:9999' | |
var xsub_url = 'tcp://0.0.0.0:9998'; | |
var noop = function(){"use strict";} | |
xpub = zmq.socket( "xpub" ); | |
xsub = zmq.socket( "xsub" ); | |
xpub.bind(xpub_url, function( err ){ | |
if( err ){ | |
process.exit(0); | |
} | |
console.log("connected to %s", xsub_url) | |
// sent no limit on messages | |
xpub.setsockopt( zmq.ZMQ_RCVHWM, 0 ); | |
xpub.setsockopt( zmq.ZMQ_SNDHWM, 0 ); | |
}); | |
xsub.bind(xsub_url, function( err ){ | |
if( err ){ | |
process.exit(0); | |
} | |
console.log("connected to %s", xsub_url) | |
xsub.setsockopt( zmq.ZMQ_RCVHWM, 0 ); | |
xsub.setsockopt( zmq.ZMQ_SNDHWM, 0 ); | |
}); | |
xpub.on("message", xsub.send.bind( xsub )); | |
//xsub.on("message", xpub.send.bind( xpub )); | |
xsub.on('message', function(){ | |
console.log('xsub:', arguments) | |
xpub.send(arguments[0], zmq.ZMQ_SNDMORE) | |
xpub.send(arguments[1], zmq.ZMQ_SNDMORE) | |
xpub.send(arguments[2]) | |
}) | |
process.on("SIGTERM", function(){ | |
"use strict"; | |
console.log("pubsub: caught SIGTERM signal") | |
console.log("pubsub: Shutting down pubsub proxy"); | |
xpub.disconnect( xpub_url ); | |
xpub.close() | |
xsub.disconnect( xsub_url ); | |
xsub.close() | |
process.exit(20); | |
}); | |
process.on('uncaughtException', function( err ){ | |
console.error("pub sub: there was an un expected error: %s", err.message ) | |
console.error( err.stack ) | |
}); |
This file contains hidden or 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 publisher = zmq.socket('pub') | |
var subscriber = zmq.socket('sub') | |
publisher.connect('tcp://localhost:9998'); | |
subscriber.connect('tcp://localhost:9999'); | |
subscriber.subscribe(''); | |
subscriber.on('message', function(){ | |
console.log(arguments); | |
}) | |
setInterval(function(){ | |
publisher.send('test', zmq.ZMQ_SNDMORE); | |
publisher.send('monday', zmq.ZMQ_SNDMORE); | |
publisher.send('tuesday'); | |
},500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment