Reputation: 7381
In the context of interactive web apps, especially those libraries utilizing technologies such as websockets, how can we transfer data structures (e.g. maps, lists, sets, etc) between the client browser and the server? The examples I've come across so far only pass strings.
Is this supported case by case dependent on the libraries used, or a more general mechanism is available?
Upvotes: 3
Views: 1597
Reputation: 73119
You can send three things over a websocket (from the client perspective):
If you have a compound Javascript data structures (hierarchy of maps and arrays) then you should use JSON to serialize them to strings and send them over the WebSocket connection as a string.
If you are interested in sending binary byte or file data over the WebSocket connection you could still serialize to a string (inefficient bandwidth-wise) or you can send the data as ArrayBuffers or Blobs.
Note 1: When sending an ArrayBuffer or Blob results in a binary WebSocket frame on the wire your server needs to support binary frames.
Note 2: The client gets to choose what type of object is returned when the server sends a binary frame. This is the binaryType property on the WebSocket object which can be set to either "arraybuffer" or "blob".
Note 3: If you browser only supports the older WebSocket Hixie protocol series (e.g. iOS Safari) then there is no binary data and you can only send and receive strings.
Upvotes: 5