socketio / socket.io-adapter

The Socket.IO in-memory adapter
https://socket.io/
197 stars 101 forks source link

Promisify socketRooms #67

Closed DavraYoung closed 3 years ago

DavraYoung commented 3 years ago

Let's make socketRooms return promise.

Sometimes it's usefull to know what rooms the specific socket is in. In single node, the Adapter.socketRooms(socketId) function works as intended.

However, with multiple socket.io servers connected through socket.io-redis adapter, it is impossible to know what rooms the socket is in, if the socket is from different node.

Making Adapter.socketRooms function to return promise will allow other adapters to implement logic of getting socket rooms even if the socket is not stored in current node.

darrachequesne commented 3 years ago

Hi! I was thinking about adding this feature with: https://github.com/socketio/socket.io-adapter/commit/1c9827ec1136e24094295907efaf4d4e6c2fef2f

Syntax (open to discussion):

const sockets = await io.in("room3").fetchSockets();
for (const socket of sockets) {
  console.log(socket.rooms); // along with id & handshake
}

What do you think?

DavraYoung commented 3 years ago

I needed to collect socket ids in order make them leave certain rooms and that functinality was covered in recent commit here. Thanks)

darrachequesne commented 3 years ago

Good news, thanks a lot for the feedback :pray: