Last active
October 1, 2023 03:21
-
-
Save esatterwhite/8770990 to your computer and use it in GitHub Desktop.
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" ) | |
, 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 )); | |
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 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.setsockopt(zmq.ZMQ_SUBSCRIBE, new Buffer("test") ); | |
subscriber.on('message', function(act, day1, day2){ | |
console.log(act.toString(), day1.toString(), day2.toString()); | |
}) | |
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