How does SocketIO have common .on () events for any given event?

When using SocketIO, any message is received like this:

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

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

How did they achieve this? How do they have an event listener for every possible line? Do they add event listeners, how do messages arrive?

I tried reading the SocketIO source code, but it went right above my head.

+4
source share
1 answer

The answer can be found in the documentation:

From the source code of Socket.IO :

socket.on(eventName, callback)

(inherited from EventEmitter)

From here :

EventEmitter , this.events, . on , EventEmitter .

, socket , , , .

EventEmitter.on, , - , :

.on() → eventName. , . , eventName .

+4

Source: https://habr.com/ru/post/1675970/


All Articles