peerjs
peerjs copied to clipboard
Some sort of `flush` before closing a connection?
I have a situation where I have data connections with multiple peers, and I want a client to send one last message to all peers before it disconnects. Whether I use close() or destroy(), the message doesn't get sent, presumably because the closure happens before the message is actually sent. But using await on the send() doesn't work, and there doesn't appear to be an explicit flush command of any sort. Is there some way of deferring the connection closure until all pending messages have been delivered? Thanks!
There’s no standard way to do that yet. But it’s a good idea, I’ll try to include it in the next release.
In the meantime, you can use the bufferSize property to check manually that the buffer is empty.
Sending happens every 50ms so checking more often than that is probably not necessary.
Any preference for await send(), on('delivered',()=>), conn.flush(), or conn.close({flush: true}) ?
Thanks for the workaround. I'll give that a try today. I don't have a particular preference on the solution. I think the simplest might be the conn.close({flush: true}). I think that would have the fewest side effects. But I'm not picky :)
I use socket.io to tell other peers to close their connection.
:tada: This issue has been resolved in version 1.5.0-rc.1 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
Starting from v1.5 (just published) you can use conn.close({flush: true}) to drain all data before closing the connection.