github.com/pion/webrtc/v4@v4.0.1/examples/data-channels-flow-control/README.md (about) 1 # data-channels-flow-control 2 This example demonstrates how to use the following property / methods. 3 4 * func (d *DataChannel) BufferedAmount() uint64 5 * func (d *DataChannel) SetBufferedAmountLowThreshold(th uint64) 6 * func (d *DataChannel) BufferedAmountLowThreshold() uint64 7 * func (d *DataChannel) OnBufferedAmountLow(f func()) 8 9 These methods are equivalent to that of JavaScript WebRTC API. 10 See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel for more details. 11 12 ## When do we need it? 13 Send or SendText methods are called on DataChannel to send data to the connected peer. 14 The methods return immediately, but it does not mean the data was actually sent onto 15 the wire. Instead, it is queued in a buffer until it actually gets sent out to the wire. 16 17 When you have a large amount of data to send, it is an application's responsibility to 18 control the buffered amount in order not to indefinitely grow the buffer size to eventually 19 exhaust the memory. 20 21 The rate you wish to send data might be much higher than the rate the data channel can 22 actually send to the peer over the Internet. The above properties/methods help your 23 application to pace the amount of data to be pushed into the data channel. 24 25 26 ## How to run the example code 27 28 The demo code (main.go) implements two endpoints (offerPC and answerPC) in it. 29 30 ``` 31 signaling messages 32 +----------------------------------------+ 33 | | 34 v v 35 +---------------+ +---------------+ 36 | | data | | 37 | offerPC |----------------------->| answerPC | 38 |:PeerConnection| |:PeerConnection| 39 +---------------+ +---------------+ 40 ``` 41 42 First offerPC and answerPC will exchange signaling message to establish a peer-to-peer 43 connection, and data channel (label: "data"). 44 45 Once the data channel is successfully opened, offerPC will start sending a series of 46 1024-byte packets to answerPC as fast as it can, until you kill the process by Ctrl-c. 47 48 49 Here's how to run the code. 50 51 At the root of the example, `pion/webrtc/examples/data-channels-flow-control/`: 52 ``` 53 $ go run main.go 54 2019/08/31 14:56:41 OnOpen: data-824635025728. Start sending a series of 1024-byte packets as fast as it can 55 2019/08/31 14:56:41 OnOpen: data-824637171120. Start receiving data 56 2019/08/31 14:56:42 Throughput: 179.118 Mbps 57 2019/08/31 14:56:43 Throughput: 203.545 Mbps 58 2019/08/31 14:56:44 Throughput: 211.516 Mbps 59 2019/08/31 14:56:45 Throughput: 216.292 Mbps 60 2019/08/31 14:56:46 Throughput: 217.961 Mbps 61 2019/08/31 14:56:47 Throughput: 218.342 Mbps 62 : 63 ```