2

I am building a javascript socket application which uses socket.io and I am new to network programming. I don't know what is the best way to pass data in the network. I understand that passing binary data is the most efficient way but it is more complicated. So I want to start with plain objects for now.

I have built a chat application before in which I just used socket.emit('message', obj) and it works perfectly fine. But I have seen some examples passing the data using socket.emit('message', JSON.stringify(obj)). I am not sure what the benefit is here. Does it make the data passing on the network smaller size? Is it more efficient than plain object?

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
newguy
  • 5,668
  • 12
  • 55
  • 95
  • What is `obj` ? Before it goes over the wire, it has to be serialized into something. Maybe socket.io does this for you under the hoods. – Thilo Nov 19 '16 at 02:56
  • It seems that `emit` already does this for you: http://stackoverflow.com/questions/11805130/socket-io-how-to-send-javascript-object (whereas `send` lets you choose how to serialize the data). – Thilo Nov 19 '16 at 02:59
  • @Thilo `obj` is the plain object. – newguy Nov 19 '16 at 03:01

3 Answers3

1

By default, socket.io will call stringify for you so it's pointless to do it manually.

Most likely, simple socket.emit('message', obj) does what you want. Even if you want to emit binary, that's how it done. It seems it "just works", there is not much documentation about it. Basically, you have to emit something that passes hasBin-check ie. it has to be Buffer, ArrayBuffer, Blob or File.

There is no Buffer.fromObject-method in Node.js API, for a reason. If you want to send your Object as binary, you have to design how to represent it as binary data. Whether or not, it's good idea, depends on what kind of data you are dealing with. If your data is mostly strings, then you can't shim it much. If you have a lot of integers, numbers, booleans etc, then using binary might be good idea, but it's quite bit of extra work to design efficient binary format.

Also, there is a format called BSON. It has browser implementations too, so technically it should be possible to serialize arbitrary Javascript Object to Buffer, send using socket.io and then deserialize it in browser. I haven't ever used it in browser environment: it sounds like interesting idea but it would need some performance testing.

Antti Pihlaja
  • 442
  • 5
  • 14
0

If you pass .emit() an object as the data paramater in socket.io, it will automatically call JSON.stringify() at the sender end and automaticlly call JSON.parse() at the receiving end.

So, you can just pass an object and the receiver will get back an object and socket.io will take care of everything in between for you automatically.

As far as I know, there is no advantage to calling socket.emit('message', JSON.stringify(obj)) yourself. Just use socket.emit('message', obj). It will not be smaller or faster or more efficient.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Since its an object instead of a string, you can use the object in other file without the needs to JSON.parse.

It makes the code simpler but like other said there are no difference physically since compiler will stringfy it anyway, maybe a smaller file size of your application.

AkiZukiLenn
  • 337
  • 3
  • 14