How to convert a Javascript object to a Node buffer?

I am using Buffer on my node server and Buffer on my Javacript client.

In order to save bytes I want to send my data to the server via websockets as binary, not JSON.

So, if I had a Javascript object [ 5, false, 55, "asdf" ] , I would like to convert it to a buffer on the client right before sending it. Maybe something like this:

 object.toBuffer('int16', 'bool', 'int16', 'utf8'); 

and read it on the server like this:

 var obj = buffer.read('int16', 'bool', 'int16', 'utf8'); 

I am considering current solutions, and it looks like I just need to do a lot of concat ing by specifying offset / length bytes, converting from ints to booleans, etc.

Is there a better way?

Edit: Here, I think you need to do this now. I think my problem is that it is too verbose and error prone, and I am looking for a more concise and elegant way to do this, because this operation will be performed in many different places in my code.

 // On client for [ 5, false, 55, "test" ] const writeFirst = Buffer.allocUnsafe(2); writeFirst.writeInt16LE(5, 0); const writeSecond = Buffer.allocUnsafe(1); writeSecond.writeUInt8(0); const writeThird = Buffer.allocUnsafe(2); writeThird.writeInt16LE(55, 0); const writeFourth = Buffer.from('test'); const result = Buffer.concat([writeFirst, writeSecond, writeThird, writeFourth]); // On server for reading buffer of [ 5, false, 55, "test" ] const readFirst = result.readInt16LE(0); const readSecond = Boolean(result.readUInt8(2)); const readThird = result.readInt16LE(3); const readFourth = result.toString('utf8', 5); 

Edit # 2:. It seems like I think I need something like protocol buffers. I'm not quite sure if they are still applied or if they are applied, but it looks like you can specify a scheme in a file for all your messages, and then serialize your JSON objects for this scheme and return a buffer to it, which you can then deserialize using same schemes on a client / other server. I am going to look at this a little more.

+7
javascript websocket buffer
source share
1 answer

The first argument of the buffer <should> should be: String, Buffer, ArrayBuffer, Array or an object that looks like an array.

Given this information, we can implement what you are looking for by creating a buffer from String . It will look something like this:

 let json = [ 5, false, 55, 'asdf' ]; let buffer = Buffer.from(JSON.stringify(json)); console.log('Buffer: ', buffer); // Buffer: <Buffer 5b 20 35 2c 20 66 61 6c 73 65 2c 20 35 35 2c 20 22 61 73 64 66 22 20 5d> 

Then you can return your JSON like this:

 let converted = JSON.parse(buffer); console.log('Parsed to json', converted); // Parsed to json [ 5, false, 55, 'asdf' ] 
+7
source share

All Articles