#Websocket broadcast

1 messages ยท Page 1 of 1 (latest)

strange wren
#

Hi,
how do I broadcast .send a message to all connected clients?

coral ginkgo
#

either socket.publish() or server.publish()

strange wren
strange wren
#

ok... I'm having some weird stuff going on, maybe it's normal and I'm just noob on this...
I've setup a websocket server to broadcast data to all connected clients when a clients connect (open(ws){ws.publish('channel',MESSAGE)})...
the issue happens when the client loses connection to the websocket server, I setup a reconnection code (socket.onclose = (e)=>{ socket = new Websocket(ADDR) }), so it should make the server publish the set MESSAGE, but it doesn't get to any clients that have reconnected without reloading the page... that's the thing I don't understand... the webpage must be reloaded for it to receive the broadcast or message... ๐Ÿค”

#

I used to use socket.io before... idk if using the socket.io client would still work with Bun's socket implementation...

coral ginkgo
#

did you subscribe that client to topic after reconnect?

#

and ws.publish() skips itself when broadcasting

strange wren
#

yeah, I set it up to open(ws){ws.subscribe(CHANNEL);ws.publish(...)} and close(ws,c,m){ws.unsubscribe(CHANNEL)} ๐Ÿค”

#

is it because I'm unsubscribing them?

#

they should subscribe again, no?

coral ginkgo
#

did you rebind .onmessage handler on your clients?

strange wren
#

wait...

#

I believe that's the issue ๐Ÿคฆโ€โ™‚๏ธ let me check...

coral ginkgo
#

socket = new Websocket() creates completely new client that has no onmessage listener attached

strange wren
#

yeah... I just realized that I did rebind the "onclose" but the "onmessaage" ๐Ÿซ 

coral ginkgo
#

(including all other events like .onclose and so on)

strange wren
#

yeah... that was the issue... ๐Ÿ˜… thanks and sorry for the time...

coral ginkgo
#

always here to help

strange wren
#

btw, is it the same to use socket.onmessage and socket.addEventListener('message', callback)??

coral ginkgo
#

yeah

#

second one probably lets you do multiple listeners

strange wren
#

oh ok... just wanted to be sure...

coral ginkgo
#

would need to check mdn to be sure