Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

381
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!