express-ws icon indicating copy to clipboard operation
express-ws copied to clipboard

[Question] How to send broadcast message?

Open 6pm opened this issue 7 years ago • 5 comments

How to send broadcast message for all users? I used before connection.sendUTF(json) with pure node server. Thank you for answer.

6pm avatar Jul 31 '17 01:07 6pm

You can get the list of all clients per the API docs, then send a message to each one:

ws.getWss().clients.forEach(client => client.send(payload));  

textbook avatar Aug 30 '17 19:08 textbook

what if I have millions connected user. Will it work efficiently?

abhishek11210646 avatar Jan 29 '18 11:01 abhishek11210646

@abhishek11210646 I wouldn't think so, it's O(n)

textbook avatar Jan 29 '18 20:01 textbook

您可以根据API文档获取所有客户的列表,然后向每个客户发送一条消息:

ws.getWss().clients.forEach(client => client.send(payload));  

How to broadcast a message to some specific users

skingorz avatar Feb 27 '19 08:02 skingorz

I answered this a bit ago. https://github.com/HenningM/express-ws/issues/140#issuecomment-900628888

Perodactyl avatar Aug 17 '21 21:08 Perodactyl