qballer
qballer

Reputation: 2111

Sharing binary buffer between Node.js server and Browser

There is an issue of how to share buffers between node.js and the browser containing binary data. I'm pretty happy with Socket.io as a transport layer but the issue is that there is no porting of the Buffer class for the browser. Not something I can find anyways

I've also came across binary.js and I was wondering if there is a good way to combine them having the socket.io as the transport layer and the Binary.js as the data medium. I also saw this question, which is kind of on topic but doesn't really resolve the issue.

I know socket.io added binary support but I haven't found any documentation on the topic.

Update:

It seems that binary.js will not be the solution. The basic requirement that I want is to share the same capabilities that Buffer has in node with the browser.

My needs consist of two things:

  1. Handle the buffer in the same manner in both Server and Browser.

  2. support Binary data.

I will probably use Array Buffer.

Edit: Since node.js run over V8 you can use ArrayBuffer. It seems as if the issue is solved. Yet, from what I know, node people decided that it's a good idea to create a buffer module and manage it in the C bindings they created (from a talk given by Ryan Dahl). I think this has to do with how buffering is done over the network. This means ArrayBuffer is still not a good data medium to share between server and browser.

Upvotes: 19

Views: 5843

Answers (2)

David Schwartz
David Schwartz

Reputation: 182761

JavaScript's built in strings use wide characters internally. So they can easily store a value from 0 to 255 in each character position. This is a JavaScript language feature, so it should work the same in a browser or in node.js.

You can use charCodeAt to extract the value of a particular position in a string and fromCharCode to create a character (that you can add to a string) with a value from 0 to 255.

You can use the various string functions to manipulate data in this form. You can create constants using JavaScript string constants like this "\x00\x12\x34\x56".

Upvotes: 2

josh3736
josh3736

Reputation: 144912

browser-buffer emulates Node's Buffer API in the browser.

It's backed by a Uint8Array, so browser support is sketchy.

Upvotes: 9

Related Questions