How to correctly unsubscribe from a room and destroy it?

I have an internal loop for each socket:

if (![hash]) { // room has expired
    delete chat.user.list[].rooms[hash];
    delete[hash]; // delete room hash from user store

socket.leave(hash) does nothing - socket still receives messages sent to hash room.

As a side note - if I connect with client Anna and client Bob - both receive messages, but if I reconnect with client Bob - Bob cannot send messages to Anna.

Is there somewhere a full socket io API documentation (as I couldn't find socket.leave(room) examples)?

EDIT: Got it! Socket IO saves room handles with slash, so you have to use socket.leave('/'+hash)



Rooms in are implicitly created and implicitly deleted. Basically they are automatically removed when they are empty.

And yes, a preceding / is added to the rooms name internally but you do not need to add this to remove a socket from a room.

Try firing console.log(io.sockets.manager.rooms) whenever a room is created to have a look at what's happening internally.


As of 0.8.7 i tried it out and it seems you don't have to add the / (slash) anymore


Works just fine.


SocketIO 1.0 has Socket.prototype.leave(roomName) and Socket.prototype.leaveAll().

And you don't have to manually remove rooms:

Upon disconnection, sockets leave all the channels they were part of automatically, and no specially teardown is needed on your part. [From here]


Sort of a tangent, but to answer the part of your question about a full documentation: Not quite, however, if you check out the source on's home page you can find some sparse documentation (view the source and CTRL+F for "rooms"). I've had to do this several times already. There's nothing about leaving rooms there, but there is some general explanation.



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.