Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to broadcast to connected clients from outside the ws app?#476

Unanswered
gheoneaemil asked this question inQ&A
Discussion options

I'm trying to broadcast messages to all clients connected to my ws, from outside.
Below is an example of what I'm trying to do :

let clients = new Set();const websockets = uWS.App().ws('/*', {    compression: uWS.SHARED_COMPRESSOR,    idleTimeout: 30,    maxBackpressure: 1024,    maxPayloadLength: 512,    open: (client) => {        clients.add(client);    },    close: (client) => {        clients.delete(client);         }});websockets.listen(SOCKET_PORT, (listenSocket) => {    if (listenSocket)         console.log('Listening to port ' + SOCKET_PORT);});tryingToBroadcastLater().then((likeNow) => {            clients.forEach(client => {                    if ( !client.send("message", true, true) )                        clients.delete(client);            });});

Any idea what I'm missing / doing wrong?

You must be logged in to vote

Replies: 2 comments 1 reply

Comment options

you should usePublish to broadcast, better performance and you dont need to add/delete from Set

open:ws=>ws.subscribe('all')app.publish('all',message)
You must be logged in to vote
1 reply
@gheoneaemil
Comment options

Thanks, it's the solution

Comment options

Link is down. Here's the updated one which has example.
https://github.com/uNetworking/pubsub-example/blob/master/uWebSockets.js/server.js#L14
Make sure to call publish on the app and not on the individual websocket connection object to send the message to all clients subscribed to a topic.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@gheoneaemil@AndeYashwanth@hst-m

[8]ページ先頭

©2009-2025 Movatter.jp