Poperton
Poperton

Reputation: 1776

Will websocket messages always arrive entirely, at once?

This websockets tutorial https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications has this example:

exampleSocket.onmessage = function(event) {
  var f = document.getElementById("chatbox").contentDocument;
  var text = "";
  var msg = JSON.parse(event.data);
  var time = new Date(msg.date);
  var timeStr = time.toLocaleTimeString();

  switch(msg.type) {
    case "id":
      clientID = msg.id;
      setUsername();
      break;
    case "username":
      text = "<b>User <em>" + msg.name + "</em> signed in at " + timeStr + "</b><br>";
      break;
    case "message":
      text = "(" + timeStr + ") <b>" + msg.name + "</b>: " + msg.text + "<br>";
      break;
    case "rejectusername":
      text = "<b>Your username has been set to <em>" + msg.name + "</em> because the name you chose is in use.</b><br>"
      break;
    case "userlist":
      var ul = "";
      for (i=0; i < msg.users.length; i++) {
        ul += msg.users[i] + "<br>";
      }
      document.getElementById("userlistbox").innerHTML = ul;
      break;
  }

  if (text.length) {
    f.write(text);
    document.getElementById("chatbox").contentWindow.scrollByPages(1);
  }
};

it parses the received message from the websockets server. However, can I trust that a json, sent by the websocket server, will always arrive at once for the client? What if it arrives partially? Parsing it would break it.

Can I make this assumption even for very large json messages?

I'm asking because on a TCP stream, this wouldn't be possible. The message could arrive partially.

If there is no way to receive the message entirely at once, how could I know when to parse a JSON?

Upvotes: 4

Views: 606

Answers (1)

Alexey Inkin
Alexey Inkin

Reputation: 2049

Yes, each message is received as a whole. From the RFC 6455:

1.2. Protocol Overview

After a successful handshake, clients and servers transfer data back and forth in conceptual units referred to in this specification as "messages". On the wire, a message is composed of one or more frames. The WebSocket message does not necessarily correspond to a particular network layer framing, as a fragmented message may be coalesced or split by an intermediary.

6.2. Receiving Data

If the frame comprises an unfragmented message (Section 5.4), it is said that A WebSocket Message Has Been Received with type /type/ and data /data/. If the frame is part of a fragmented message, the "Application data" of the subsequent data frames is concatenated to form the /data/. When the last fragment is received as indicated by the FIN bit (frame-fin), it is said that A WebSocket Message Has Been Received with data /data/ (comprised of the concatenation of the "Application data" of the fragments) and type /type/ (noted from the first frame of the fragmented message). Subsequent data frames MUST be interpreted as belonging to a new WebSocket message.

https://www.rfc-editor.org/rfc/rfc6455

The confusion might come from the term 'socket' which is a low-level raw OS channel that yields data in chunks. However, WebSocket is a higher level protocol.

Upvotes: 4

Related Questions