peerjs icon indicating copy to clipboard operation
peerjs copied to clipboard

Some sort of `flush` before closing a connection?

Open Perlkonig opened this issue 3 years ago • 3 comments

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!

Perlkonig avatar Jul 07 '22 01:07 Perlkonig

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}) ?

jonasgloning avatar Jul 07 '22 08:07 jonasgloning

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 :)

Perlkonig avatar Jul 07 '22 16:07 Perlkonig

I use socket.io to tell other peers to close their connection.

rushglen avatar Jul 11 '22 03:07 rushglen

: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:

github-actions[bot] avatar Aug 14 '23 13:08 github-actions[bot]

Starting from v1.5 (just published) you can use conn.close({flush: true}) to drain all data before closing the connection.

jonasgloning avatar Sep 03 '23 10:09 jonasgloning