Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io Removing specific listener

People also ask

How do I remove a listener from socket IO?

off(eventName, listener)​ Removes the specified listener from the listener array for the event named eventName.

How do I emit to a specific user socket IO?

To send a message to the particular client, we are must provide socket.id of that client to the server and at the server side socket.io takes care of delivering that message by using, socket.broadcast.to('ID'). emit( 'send msg', {somedata : somedata_server} ); For example,user3 want to send a message to user1.

What is the difference between WebSocket and socket IO?

Key Differences between WebSocket and socket.ioIt provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback. WebSocket is the technology, while Socket.io is a library for WebSockets.

What is emit in socket IO?

emit() to send a message to all the connected clients. This code will notify when a user connects to the server. io.on("connection", function(socket) { io.emit(“user connected”); }); If you want to broadcast to everyone except the person who connected you can use.


//To unsubscribe all listeners of an event
socket.off('event-name');

//to unsubscribe a certain listener
socket.off('event-name', listener);

Note that socket.off, socket.removeListener, socket.removeAllListeners, socket.removeEventListener are synonyms.

This is tested on socket.io v1.4.3


You need to pass in the listener function to removeListener.

function testFun(data){
    console.log('test complete',data);
}

socket.on('testComplete', testFun); 

function emitTest(){
    console.log('emitting test');
    socket.emit('test','first emit');
}

function removeListener(){
    socket.removeListener('testComplete', testFun);
}