This section will cover best practices for WebSockets based applications. The following topics will be covered:
Throttling the rate of sending data
Controlling the maximum size of the message
Working with proxy servers and WebSockets
After the WebSocket connection is opened, messages can be sent using the send function.
WebSockets have a bufferedAmount
attribute that can be used to control the rate of sending data. Using the bufferedAmount
attribute you can check the number of bytes that have been queued but not yet sent to the server.
Here is a snippet to test for the bufferedAmount
attribute of WebSocket.
// This snippet checks for amount of data buffered but not sent yet // in case it is less than a predefined THRESHOLD the webSocket // can send the data if (webSocket.bufferedAmount < THRESHOLD) webSocket.send(someData); };
This can be done periodically using the setInterval
function. As you can see...