An overview of the

This article is the sixth chapter of WebSocket protocol. The main content of this article is WebSocket message sending and receiving.

Those interested in the previous chapters of this document can see:

  • WebSocket Protocol — Abstract
  • WebSocket Protocol Chapter 1 — Introduction
  • WebSocket Protocol Chapter 2 — Conformance Requirements
  • WebSocket Protocol chapter 3 — WebSocket URIs
  • WebSocket Protocol chapter 4 — Opening Handshake
  • WebSocket Protocol Chapter 5 — Data Framing

Sending and receiving messages (Protocol body)

6.1 Sending Data

To send a WebSocket message over a WebSocket connection, an endpoint must follow the following steps:

  1. An endpoint must ensure that the WebSocket connection is OPEN (see sections 4.1 and 4.2.2). If the state of either end of the WebSocket connection changes, the terminal must abort the following steps.
  2. An endpoint must encapsulate the data according to the WebSocket frame defined in Section 5.2. If the amount of data that needs to be sent is too large or if the data is not available at the point of completeness when the endpoint wants to start sending messages, then the endpoint may choose to encapsulate the data through a series of frames defined in Section 5.4 (p.
  3. The first frame opcode (frame opcode) containing the data must be set to an appropriate value as described in Section 5.2 so that the receiver can parse the data into text or binary data.
  4. The FIN (FIN frame) field of the last frame containing data must be set to 1 as defined in Section 5.2.
  5. If data is sent to the client, the data frame must be masked as defined in Section 5.3.
  6. If there are negotiated extensions in a WebsSocket connection (Chapter 9), definitions and considerations in these extensions may need additional consideration.
  7. The formatted frames must be transmitted over the underlying network connection.

6.2 Receiving Data

In order to receive WebSocket data, the terminal needs to listen for the underlying network connection. The entered data must be parsed through the WebSocket frame defined in Section 5.2. If a control frame is received (Section 5.5, p. 65), the frame must be processed as defined in Section 5.5. If a data frame is received, the endpoint must pay attention to the data types defined in the opcodes (frame opcodes) in Section 5.2. The “application data” in this frame is defined as message data. If the frame contains unsharded data (Section 5.4, p. 59), it is considered that the data and type of a WebSocket message were received. If a frame is part of the shard data, then subsequent frames contain “application data” that are linked together to form the data. When the last fragment represented by the FIN field (FIN frame) is received, we can say that the data (composed of the “application data” data assembled by the fragment) and the type (note the first frame of the shard message) of a WebSocket message have been received. The next data frame must belong to a new WebSocket message.

Extensions (Chapter 9) may change how data is understood, including specifically the content boundaries of messages. Extension, in addition to adding Extension Data before Application Data, you can also modify Application Data (for example, compress it).

As mentioned in Section 5.3, the server must remove the mask when it receives a data frame from the client.