1

I want to send objects between client and server. however, when i try to pass an object, i find that i am unable to use it (values get undefined).

To get around this, i am simply passing the object through the connection as a string by using

json.stringify at source

and

json.parse at destination

this is working so far but i am worried this is an inelegant solution and when i start working with complex object (graphics objects etc), i will end up in a bad place.

Is there any native way in sockjs for working with objects?

thanks.

Rishav Sharan
  • 2,763
  • 8
  • 39
  • 55
  • 1
    possible duplicate of [sending a javascript object through websockets with](http://stackoverflow.com/questions/13028604/sending-a-javascript-object-through-websockets-with) – franzlorenzon Jun 25 '14 at 08:44

1 Answers1

1

Using network, everything is transferred in binary form 0/1. Regardless of what data is (image, json, string, audio, video, etc..).

The process of converting from one form to another is called Serialisation/Deserialisation. WebSockets by default support binary and text based messages. Text is utf-8, so it will be overkill to send images in representation of characters as text, or even base64 is not the best way as it require more than one conversion.

For binary type of data (images, audio, video) it should be sent as binary (Blobs), and strings should be sent as text.

JSON - is text based human readable format and parsers are pretty fast today so it is "OK" to stringify and parse it to send around. If there is actual problem with performance atm, then you need to address as your logic as well as data protocol here, consider using protobuff or bson to send in more efficient way data around.

But you should not worry about this on early stage of development.
It is way better to work on real problems.

moka
  • 22,846
  • 4
  • 51
  • 67