Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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;
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

EventEmitter instances can't reach beyond the bounds of a process. If you want to communicate between parent and children, use worker.send():

// cluster.js
setInterval(function () {
  for (const id in cluster.workers) {
    cluster.workers[id].send({ type : 'testTimer', data : { tester : 'test' }});
  }
}, 1000);

// server.js
process.on('message', function(message) {
  if (message.type === 'testTimer') { 
    console.log('Starting Sync ', message.data);
  }
})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda