Is RTCDataChannel send() a synchronous/blocking call?

970 Views Asked by At

I'm trying to send a file in chunks over WebRTC, and I'm wondering if I can create a callback function to be called after RTCDataChannel.send() finishes sending each chunk of the file.

Is RTCDataChannel.send() a synchronous/blocking call? If so, my callback can be executed on the line after .send().

If .send() is asynchronous/non-blocking, then this will get tricky since it doesn't seem like .send() accepts a callback function, and I want to avoid using a buffer and a timeout.

2

There are 2 best solutions below

1
On BEST ANSWER

The send method is blocking. It however doesn't wait until the data went over the wire, but only puts the data on an internal buffer from where it might later (or in parallel to the script execution) be sent.

The amount of data that has not been transmitted is available as the bufferedAmount property, which will be synchronously increased by every send() call (and not be updated otherwise until the next event loop turn).

You might make your wrapper asynchronous therefore, and put a timeout before actually calling send() when the currently buffered data is "too much" (by whatever criterion you see fit).

0
On

As noted above send() is effectively async - you don't get delivery receipt. However there is a callback onbufferedamountlow which is invoked when the channel drains it's send buffer below a value set with bufferedAmountLowThreshold (see MDN onbufferedamountlow)

You can use that callback to decide when to send the next chunk.

Note however that this is relatively new to the draft standard and may not be supported everywhere.