1

I use sockjs for work with Websocket.

sock.send( JSON.stringify(obj1) );
sock.send( JSON.stringify(obj2) );
sock.send( JSON.stringify(obj3) );
...

Is it guarantied, that "send" commands will be evaluated one after another? (when first finishes, the second starts and so on)

Thanks in advance!

2 Answers 2

2

Data to be sent is queued and transmitted asynchronously, so the second one might be called before the first's data has been transmitted. However, data sent by the second call to send will not reach the server before the first.

You can read more about Web Socket behaviour in the HTML5 specification.

Sign up to request clarification or add additional context in comments.

Comments

0

If your send calls are in the same Javascript execution context then they will definitely be executed in order. In addition, each send will be received as a whole message by the onmessage handler (i.e. it will not get fragmented).

Same execution context:

function doit () {
    ws.send("msg1");
    ws.send("msg2");
}

Different execution context:

setTimeout(function () { ws.send("msg1"); }, 100);
setTimeout(function () { ws.send("msg2"); }, 110);

In the second example, the "msg2" is not guaranteed to be delivered after the "msg1". It is likely since the delay specified is slightly longer, but not guaranteed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.