go-socket.io icon indicating copy to clipboard operation
go-socket.io copied to clipboard

BroadcastToRoom blocks until all messages have been emited, causing delays with slow connections

Open lianmengworld opened this issue 4 years ago • 7 comments

Describe the bug "broadcast to room" will block broadcasting for a short time

lianmengworld avatar Jun 11 '20 17:06 lianmengworld

Seems to be related to this: https://github.com/googollee/go-socket.io/blob/master/broadcast.go#L93 Not sure why it's locking even during the .Emit. I assume it only needs to block while actually reading the current list of connections. Been trying to release the lock during Emit, running into other issues in production, so can't say if it works.

erkie avatar Sep 11 '20 06:09 erkie

To add another data point, I ran into this while trying the simple example of broadcasting to multiple clients when a client has disconnected - it blocks entirely until the read timeout is hit.

jtcho avatar Sep 11 '20 10:09 jtcho

It seems to be a fairly easy fix. Not letting the .Emit call in broadcast.Send block by using go-routines. This is how the node-implementation works.

https://github.com/googollee/go-socket.io/blob/0c0217c03e1d56780f85e83696203837b62f58ef/broadcast.go#L101

// change
connection.Emit(event, args...)
// to
go connection.Emit(event, args...)

@mrfoe7 what are your thoughts here?

erkie avatar Sep 16 '20 07:09 erkie

It seems to be a fairly easy fix. Not letting the .Emit call in broadcast.Send block by using go-routines. This is how the node-implementation works.

https://github.com/googollee/go-socket.io/blob/0c0217c03e1d56780f85e83696203837b62f58ef/broadcast.go#L101

// change
connection.Emit(event, args...)
// to
go connection.Emit(event, args...)

@mrfoe7 what are your thoughts here?

Has anyone tried this solution? Is that OK?

Karafs avatar May 24 '21 13:05 Karafs

We're running this in production for a long time

erkie avatar May 24 '21 13:05 erkie

We're running this in production for a long time

thanks.

Karafs avatar May 24 '21 14:05 Karafs

Has the problem been solved yet?

tqrj avatar Apr 17 '23 09:04 tqrj