18

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.

Community
  • 1
  • 1
qballer
  • 2,033
  • 2
  • 22
  • 40
  • 1
    I don't understand what exactly the problem is. What functionality are you exactly interested in that does not exist in the browser? – Benjamin Gruenbaum Sep 01 '12 at 18:47
  • My needs consist of two things: 1. Handle the buffer in the same manner in both Server and Browser. 2. Support Binary data. – qballer Sep 01 '12 at 18:53
  • 1
    @BenjaminGruenbaum: The problem is not that functionality isn't available in the browser, it's that the functionality that is available in the browser (typed arrays) isn't available in `node.js`. He doesn't want to have to write all his code twice. – David Schwartz Sep 18 '12 at 21:56
  • Hi @DavidSchwartz thanks for promoting this question. I've added some details to clarify the Array Buffer issue. – qballer Sep 20 '12 at 11:19
  • @qballer: Is ArrayBuffer available in the most prevalent versions of IE and `node.js`? If so, that sounds like a solution. – David Schwartz Sep 20 '12 at 15:35
  • 1
    Nope, ArrayBuffer will only come out with IE10. There is still a performance issue with buffering a data structure from the V8. That is why node.js run the buffer module outside of V8 – qballer Sep 20 '12 at 15:55
  • Interesting topic, I'm curious about using BinaryJS with Socket.io for backwards compatibility with http sockets. I don't have enough background knowledge about the two at this moment to say much, but it would definitely be interesting. – Shawn Khameneh Oct 08 '12 at 20:37

2 Answers2

8

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

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

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • This is the kind of answer I'm looking for, the thing is, it's still very unstable. – qballer Sep 25 '12 at 13:35
  • Yeah, there's very little activity in the project; it looks like it's someone's experimental code. I'd use it as a good starting point -- you're going to have to fork and improve the code. – josh3736 Sep 25 '12 at 17:45
  • 2
    These days this lives here: https://github.com/feross/buffer – Arto Bendiken Mar 31 '21 at 22:05
2

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".

David Schwartz
  • 179,497
  • 17
  • 214
  • 278