Version 4 io.sockets.connected returns undefined #3993
-
I had an app that used io.sockets.connected[target.socketId].leave(room) to kick someone out of a room via their socket.id. I upgraded to 4.1.0 recently on a new app and using the same code it doesn't work, and when I tried console.log(io.sockets.connected) it says undefined. What's the proper way to kick someone out of a room by their socket ID now? I can't seem to find it anywhere in the documentation. I've only found how to make the requested user leave by their socket. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The You can either:
if (io.sockets.sockets.has(target.socketId) {
io.sockets.sockets.get(target.socketId).leave(room);
} Reference: https://socket.io/docs/v4/server-api/#namespace-sockets
io.in(target.socketId).socketsLeave(room); Reference: https://socket.io/docs/v4/server-api/#server-socketsLeave-rooms |
Beta Was this translation helpful? Give feedback.
The
connected
attribute was indeed removed in[email protected]
.See here: https://socket.io/docs/v4/migrating-from-2-x-to-3-0/#Namespace-connected-is-renamed-to-Namespace-sockets-and-is-now-a-Map
You can either:
sockets
attribute insteadReference: https://socket.io/docs/v4/server-api/#namespace-sockets
socketsLeave
method (preferred way, as it works in a multi server setup)Reference: https://socket.io/docs/v4/server-api/#server-socketsLeave-rooms