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.

7
  • 1
    I don't understand what exactly the problem is. What functionality are you exactly interested in that does not exist in the browser? Sep 1, 2012 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 1, 2012 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. Sep 18, 2012 at 21:56
  • Hi @DavidSchwartz thanks for promoting this question. I've added some details to clarify the Array Buffer issue.
    – qballer
    Sep 20, 2012 at 11:19
  • 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, 2012 at 15:55

2 Answers 2

8
+125

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

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

3
  • This is the kind of answer I'm looking for, the thing is, it's still very unstable.
    – qballer
    Sep 25, 2012 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, 2012 at 17:45
  • 2
    These days this lives here: github.com/feross/buffer Mar 31, 2021 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".

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.