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

388
Vistas
How does SocketIO have generic .on() events for any given event?

When using SocketIO, receiving any message is done as so:

socket.on("eventname", function() {
    // Whatever
}

But "eventname" can be literally any string of text.

How did they achieve that? How do they have an event listener for each and every possible string? Do they add event listeners AS the messages come in?

I've tried reading the SocketIO source code, but it went right over my head.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The answer can be found looking at the documentation:

From the source code of Socket.IO:

socket.on(eventName, callback)

(inherited from EventEmitter)

From here:

Internally, the EventEmitter keeps track of an object called this.events that maps event names to arrays of event handlers. When a program calls the on method with an event name and event handler, the EventEmitter adds the handler to the array for that event name.

So basically, the socket keeps a map of events, keyed by name, to an array of functions that are called when that event is received.

And if you look at the documentation of EventEmitter.on, you'll see that it is pretty dumb - it does not check that you aren't adding the same function twice:

.on() -> Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

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