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.