Binary data via websocket without encoding in UTF-8 or base64

I am trying to use websockets in binary mode to transfer binary data from a server written in Python to a client browser (javascript is working). I implemented this link in text mode, but now I want to improve performance by communicating in binary mode.

All the examples I've seen (like this , as well as if you are looking at the source code of matplotlib / webagg that Tornado uses) they use binary mode, but in fact they seem to encode raw binary data in UTF-8 (or base64) at some point before the transfer. In my opinion, this is not true binary translation, as it adds overhead from 30% to 50%.

So my question is: do I need to encode binary data in utf-8 or base64 in order to use IP based websites? If not, please indicate an example where this is done without encoding.

I have always believed that sockets support true binary communication, but perhaps this is not the case for IP networks for some reason. Perhaps someone can shed light on this issue. It seems that success in this area was achieved last year, which adds to the confusion.

+4
source share
1 answer

IETF 6455 WebSocket Protocol supports direct send / receive of binary data (the older version of the Hixie protocol did not support). If you are doing your own framing, you just need to set the opcode in the frame to 0x2 to indicate that the payload is raw binary data, not UTF-8 encoded text. If you are using the python WebSocket library, you need to use the API provided by this library to select binary mode (if the library supports it).

Note : the example you are associated with is not a WebSocket example (it is a regular client and TCP server). Also, this example is not encoding data like UTF-8 or base64. Websockify is a WebSocket server that supports direct binary data (in addition to base64 encoding for the older version of Hixie). Disclaimer: I created websockify.

+9
source

All Articles