Why are the messages sent over WebRTC received in a different order sometimes?

arraybuffer, rtcdatachannel, webrtc

Solution

In 2016/11/01 , there is a bug that lets the `dataChannel.bufferedAmount` value change during the event loop task execution. Relying on this value can thus cause unexpected results. It is possible to manually cache `dataChannel.bufferedAmount`, and to use that to prevent this issue.

See https://bugs.chromium.org/p/webrtc/issues/detail?id=6628

Problem

I use ordered set to true, however when many (1000 or more) messages are sent in a short period of time (< 1 second) the messages received are not all received in the same order. ``` rtcPeerConnection.createDataChannel("app", { ordered: true, maxPacketLifeTime: 3000 }); ``` I could provide a minimal example to reproduce this strange behavior if necessary. I also use bufferedAmountLowThreshold and the associated event to delay when the send buffered amount is too big. I chose 2000 but I don't know what the optimal number is. The reason I have so many messages in a short period of time is because I don't want to overflow the maximum amount of data sent at once. So I split the data into 800 Bytes packs and send those. Again I don't know what the maximum size 1 message can be. ``` const SEND_BUFFERED_AMOUNT_LOW_THRESHOLD = 2000; //Bytes rtcSendDataChannel.bufferedAmountLowThreshold = SEND_BUFFERED_AMOUNT_LOW_THRESHOLD; const MAX_MESSAGE_SIZE = 800; ``` Everything works fine for small data that is not split into too many messages. The error occurs randomly for big files only.

Original source