Unbind the specified event handler (opposite of .on()
).
io.socket.off(eventIdentity, handlerFn);
This method is here for completeness, but most apps should not need to use it. See below for more information.
Argument | Type | Details | |
---|---|---|---|
1 | eventIdentity |
The unique event identity associated with a server-sent message, e.g. "recipe" | |
2 | handlerFn |
The event handler function to unbind from the specified event. |
- If you decide to use this method, be careful!
io.socket.off()
does not stop the this client-side socket from receiving any server-sent messages-- it just prevents the specified event handler from firing. Usually, the desired effect is to prevent messages from being sent altogether, which is critical if your server-sent messages contain private data. This happens automatically when a socket disconnects, but there are also rarer use cases where it is necessary to unsubscribe sockets from rooms while they are still connected. For example, consider a scenario where an admin user is banned from your system while viewing a realtime dashboard, and your app needs to prevent them from receiving all subsequent realtime updates. To force a client socket to stop receiving broadcasted messages, do not use this method. Instead, unsubscribe the socket in your server-side code:
- If the room was joined using
sails.sockets.join()
, callsails.sockets.leave()
.- If the room was joined using resourceful pubsub methods, call
.unsubscribe()
or.unwatch()
, whichever is appropriate.- In order to use
.off()
, you will need to store thehandlerFn
argument you passed in to.on()
in a variable.