Prasanna
Prasanna

Reputation: 2641

Abstracting socket io server from event name

I am trying to create a SocketIO server that can listen to a variable event name (that a client will initiate) like

socket.on( X, function(msg){ // X = any event name provided by the client
    io.emit(X, msg); // Emit events to all the clients listening to the same event name
});

For example, if the client emits an event like "eventX", server should be able to listen to "eventX" and emit to all other clients that are connected to the server listening to "eventX".

My idea here is that, Server should not care about the name of the event that a group of clients use and the same piece of server code should be able to emit events when a different name is used by the group of clients for communication.

Upvotes: 0

Views: 1071

Answers (1)

jfriend00
jfriend00

Reputation: 707248

You can very easily do this by creating one master message name that the server listens for and then have the first argument when it is sent by a client be a sub-message name of the client's choosing. Other arguments could be sent that are specific to the sub-message name. That could meet your requirements.

Upvotes: 3

Related Questions