Wednesday, 26 April 2017

Nodejs events captured in any cluster process sent from cluster creating file

I am stuck here due to a simple event related issue. Here is the issue:

  • I have created a cluster using cluster.js and forked server.js from cluster.js.
  • I have put a timer from cluster.js and after every 1 min I am triggering an event 'testTimer'. I have used a event file to do
    it.
  • I am trying to capture this event 'testTimer' from the child
    process using the same file I have imported into server.js and doing a .on('testTimer', callback)

However, the events are not captured in any of the processes. I have tried making the event global and assign the event globally to a symbol but was unable to get it work/capture event as well.

Here is the codes:

cluster.js (child process creator)

...require > events.js...
... create cluster logic...
setInterval(function () {
 evt.emit('testTimer', {tester: 'test'});
 evt.tester();
}, 1000);

server.js (child process)

...require > events.js...
evt.on('testTimer', function (data) {

    console.log('Starting Sync ', data);
});

events.js (common file for events)

var util         = require("util");
var EventEmitter = require("events").EventEmitter;

function test () {
    EventEmitter.call(this);
}
test.prototype.tester = function (){
    this.emit('testTimer', {missed: 'this'})
}
util.inherits(test, EventEmitter);
module.exports = test;



via Gary

No comments:

Post a Comment