user2727195 user2727195 - 2 months ago 9
Javascript Question

socket.io events and custom events

connection
disconnect
custom-event


Are these string available somewhere on the
socket
object on the server-side? in case the socket object is passed around, otherwise I'll passing another param with event as well and function signature would be as follows.

io.on('connection', function(socket) {
doSomething(socket, 'connection');

socket.on('disconnect', function(){
doSomething(socket', 'disconnect');
}
}


function doSomething(socket, eventName) {
// is there a way to get this event name from socket object itself?

}

Answer

Are these string available somewhere on the socket object on the server-side?

No. If you want an event name passed to your function, you need to pass it yourself like your own example shows. It is not part of the socket object because (with async operations in-flight) multiple events could be in-flight at the same time so the socket object is not a safe place to store the "current" event.

Comments