Well, I have reduced my problem to the following:
I have a node.js
server sending responses to my WebSocket
client (browser) at an interval of 2s. The client code to receive the response is as follows:
socket.onmessage = function (message) {
//display message
}
In most cases, I can receive a response in at least 2s on the client. However, sometimes due to poor network connection or high server load, I can get a burst of responses from the server. For example, instead of one response in 2s, I get 3 responses at once in 6s.
How can I handle the situation at the client so that a delay of at least 2s is preserved?
One option is to queue all the responses in the client and then have a timer that periodically checks the queue. Is there a better way?
Also is the function onmessage
thread-safe? That is, does a new response from the server blocks while the code block is being executed?