Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What 's different between "text frame" and "binary frame" in Websocket?

Tags:

websocket

Text frame(opcode = 0x01) and Binary frame(opcode = 0x02) in RFC 6455. What's different between them and which one is faster?

like image 780
Thế Hải Nguyễn Avatar asked Dec 21 '16 09:12

Thế Hải Nguyễn


People also ask

What is binary message in WebSocket?

WebSockets support sending binary messages, too. To send binary data, one can use either Blob or ArrayBuffer object. Instead of calling the send method with string, you can simply pass an ArrayBuffer or a Blob . // Sending file as Blob var file = document. querySelector('input[type="file"]').

What is a WebSocket frame?

The websocket protocol communicates with frames. Frames are a header + application data. The frame header contains information about the frame and the application data. The application data is any and all stuff you send in the frame “body”.

Which protocol is used by WebSockets?

The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].

What is the mask in a WebSocket frame?

The reason for the masking is to make websocket traffic look unlike normal HTTP traffic and become completely unpredictable. Otherwise any network infrastructure equipment which is not yet upgraded to understand the Websocket protocol can mistake it for normal http traffic causing various problems.


1 Answers

For some background and perhaps more familiarity in other realms, HTTP/1 relied on an unstructured plaintext protocol while HTTP/2 allowed for faster processing of messages through binary framing. Also, SMTP relies on text while TCP relies on a binary protocol. To sum up, text relies on ASCII ('Hello') while binary, although a confusing term, has no readable representation. In JavaScript, socket.send(new ArrayBuffer(8)) is a basic binary object and is one example of the format that can be sent. This allocates a contiguous memory area of 8 bytes and pre-fills it with zeroes.

Hopefully this provides some good context.

0x01 is hexadecimal number (denoted by 0x and represents the integer 1) is the 4 bit opcode for the receiver (client or server) to know what type of "frame" it will receive. UTF-8 is denoted by 0x01 and raw binary is denoted by 0x02.

Raw binary will be much faster. Imagine an architecture like so:

Request Response Architecture

If we send textual data (opcode 0x01) via the Websocket, it must translate the received data at the proxy. Before the TCP server responds to the client message, it may have to translate the response to textual data. Encoding binary data to ASCII text via base64 will increase the size of the message by 20-30%.

With an opcode of 0x02, we can skip 2 steps in the whole request/response cycle and reduce the size of the message we are passing. In short, we skip interpretation. Moreover, the Websocket spec has UTF-8 validation rules. The difference between text and binary might be 2x in favor of binary.

like image 93
snewcomer Avatar answered Dec 23 '22 22:12

snewcomer