Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript typed arrays "over the wire"

Im creating a 8 bit unsigned javascript array:

 var myArray = Uint8Array(64);

Manipulating this array on both client and server, then sending it over a socket.io connection. We are writing a game thus the data sent over the wire as small as possible. as socket.io does not support sending binary data is it worth bothering with javascript typed arrays or should we just use normal javascript arrays? will they still be smaller then a native js array?

like image 282
AndrewMcLagan Avatar asked Oct 04 '12 00:10

AndrewMcLagan


1 Answers

NOTE: I will assume that by client you mean the browser. Else, please clarify with more details.

Socket.io does not support binary data, mainly because it offers different transports, and many of them do not support it.

However, native websockets DO support Blobs and ArrayBuffers.

If you really want to go with binary data for efficiency (which, I agree, is the way to go in your case), I think you should consider using websockets instead of socket.io.

The bad:

  • Only ~55% of users browse the web with a browser that supports websockets.
  • You wouldn't have the commodities socket.io offers, such as channels, emit and on methods.

The good:

  • Web sockets API is extremely simple.

  • It will be much more memory efficient. Normally your normal arrays are transferred by first making them a JSON string and then sending them back. This means you're actually sending a string representation of your array! Instead, here you will send the amount of bytes you would expect (in a more predictable manner, without checking string lengths before sending, but in a more "protocol"-ic way if desired).

If you decide to use WS, you could check this: http://www.adobe.com/devnet/html5/articles/real-time-data-exchange-in-html5-with-websockets.html

Else you can just go with JSON.

Truth be told, if you still go with JSON for socket.io and "universal" support, enable flash transport too, and disable slower transports if the game requires low latency.

like image 113
Mamsaac Avatar answered Nov 05 '22 06:11

Mamsaac