B
B
Boris Lapin2015-01-04 23:15:05
Python
Boris Lapin, 2015-01-04 23:15:05

Does it make sense to use BSON instead of JSON when communicating over WebSocket?

In one of the projects that I am now starting to do, the task is to send data using the WebSocket protocol. On the client, the usual "web face" with javascript logic and Tornado is used as the server. By default, Tornado works with JSON (there is a built-in deserializer and serializer). JSON is perfectly processed by Javascript and does not require any dancing with a tambourine.
So. Does it make sense to use the BSON binary format instead of JSON?
At the moment I see the following advantages :

  1. less traffic between client-server
  2. support for more data types
  3. Kind of like faster format processing.

And the cons found :
  1. the need to use additional libraries / modules to work with the format
  2. because BSON data comes to the client in blob/ArrayBuffer format, then it has to be converted to Uint8Array (TypedArray in short). This conversion does not inspire confidence in me. Not productive. On the other hand, maybe there is a more elegant solution to this problem? What do you advise?

Right now I'm receiving the server's response like this:
wsconnection = new WebSocket(---);
wsconnection.binaryType = 'arraybuffer';
wsconnection.onmessage = function (event) {
    console.log(BSON.deserialize(new Uint8Array(event.data)));
}

Maybe there is a smarter solution? To be or not to be?
It is supposed to send mostly small data arrays. But I would also like to be able to send files within the format (I mean not static. The static will be controlled by nginx, if you thought ...).
UPD:
Updated the deserialization code.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
Boris Lapin, 2015-01-07
@MrBoriska

In general, it makes no sense. On the server, processing is a little faster. On the client noticeably slower. The weight is 2% less ... But at the same time, it is also not readable. BSON instead of JSON - the answer is no.
We tried MessagePack. Speed ​​tests gave the best results, but it was not possible to start transmission on this format. Why is unknown. The unpackb function returns an error.
Conclusion:
Thank you all

K
koshak, 2015-01-05
@koshak

Look at msgpack .

A
Alexey Yeletsky, 2015-01-05
@Tiendil

And at least one of the listed pluses is critical for you? So, are you really into it?
If yes, use it, if not, don't.
KISS

E
Eternalko, 2015-02-06
@Eternalko

KISS +1. I don't think it makes sense. We use sockets ourselves, there is no point in BSON. Between servers, yes, there is a lot of traffic there.
> This conversion does not inspire confidence in me. Not productive.
In vain. Binary operations are the fastest you can get.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question