I am trying to create a set of peer-to-pear hooks that are sending
events to each other (essentially broadcasting events across node
processes/hosts)
I wrote a noddy little pub/sub example (code below) that listens to
messages & sends messages periodically.
I can start a number of them on the same host, and they all receive
each others messages happily (my example also listens to its own
messages).
I can then kill the current "master"/"server" and, as one would
expect, one of the peers takes over as server and all messages are
sent/received as expected.
All great so far.
However, I start to see unexpected behaviour if I kill the expected
successor.
If I start, say, 4 of them, and we have hooks: "hooky", "hooky-0",
"hooky-1", "hooky-2".
"hooky" will be the master & "hooky-0" will be the new master if
"hooky" dies.
If I kill "hooky-0", then kill "hooky" : Rather than "hooky-1"
becoming the new server (as I would expect), I find there is no new
server the remaining hooks no longer receive any messages.
If I kill "hooky-0" and restart it again before killing "hooky": the
behaviour seems unpredictable - sometimes messages continue being
received - sometimes not (I havent figured out a pattern yet).
Is this behaviour expected? Is there something I have misunderstood?
Cheers.
Here is my code:
===============================
var Hook = require('
hook.io').Hook;
var hooky = new Hook({
name: "hooky",
silent: false,
autoheal: true
});
hooky.on('hook::ready', function () {
console.log(
this.name + " ready");
});
hooky.on('*::hello', function (data) {
console.log(
this.name + ' received data ', data);
});
hooky.on('hello', function (data) {
console.log(
this.name + ' received data ', data);
});
function hookySend() {
var hello = 'from ' +
hooky.name + ' ' + new Date().toString();
hooky.emit('hello', {hello: hello});
}
hooky.start();
setInterval(hookySend, 2000);