Create a list of Connected Clients using socket.io

前端 未结 2 1384
野性不改
野性不改 2020-12-24 09:36

Here are 2 related questions. Posting them together makes more sense.

Question 1

I have a node.js app which emits an event out to all client

2条回答
  •  时光取名叫无心
    2020-12-24 10:25

    Socket.io provides you with a public api for that, so instead of hacking something up like Bryan suggest you can use:

    io.sockets.clients()
    

    That will returns an array of all connected clients.

    If you want all clients connected to a certain namespace:

    io.of('/namespace').clients()
    

    But you can even filter it even more.. if you want to have all sockets in a room:

    io.sockets.clients('room name here as first argument')
    

    Will return a array of connected sockets for the room room name here as first argument

提交回复
热议问题