Skip to content

Instantly share code, notes, and snippets.

@abuzarhamza
Last active May 27, 2017 20:20
Show Gist options
  • Save abuzarhamza/adb137699b332b13cfe2dbe8d04ebb4c to your computer and use it in GitHub Desktop.
Save abuzarhamza/adb137699b332b13cfe2dbe8d04ebb4c to your computer and use it in GitHub Desktop.
zmq-publisher.js
const fs = require('fs'),
zmq = require('/usr/local/lib/node_modules/zmq'),
filename = 'pub.txt';
const pub = zmq.socket('pub');
//watch the change in the file.
//when you want to sned the message make change in pub.txt
fs.watch(filename, function() {
//message send to subscriber with cat
pub.send( [ 'cat' , JSON.stringify({
type: 'change',
file: filename,
time: Date.now()
})
]);
//message send to subscriber with dog
setTimeout( function () {
pub.send(['dog' ,JSON.stringify({
type: 'change',
file: filename,
time: Date.now()
})
]);
} , 10000)
});
pub.bind('tcp://*:3000', function() {
console.log("Publisher listening to port 3000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment