Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send binary data from a Node.js socket.io server to a browser client?

I've been looking through the entire Socket.IO docs, but, even though they promise it is there, I can't find a simple, minimal example, of how one would send binary data between server/client.

How is it done?

like image 665
MaiaVictor Avatar asked Jun 05 '14 22:06

MaiaVictor


2 Answers

It is in fact in the documentation. The current documentation for Socket.io says under Socket.emit:

[...] Emits an event to the socket identified by the string name. Any other parameters can be included. All datastructures are supported, including Buffer [...]

So, if you can send a buffer, you can send binary data. All you have to do is to pack your data into a Buffer object.

You may want to read Socket.io Binary Support and Sending and Receiving Binary

like image 79
Edwin Dalorzo Avatar answered Sep 20 '22 00:09

Edwin Dalorzo


Starting from socket.io 1.0 it is possible to send binary data. http://socket.io/blog/introducing-socket-io-1-0/

How ever the way of sending and receiving binary data is not clear in the official documentation. The only documentation is:

var socket = new WebSocket('ws://localhost');
socket.binaryType = 'arraybuffer';
socket.send(new ArrayBuffer);

I suggest you to take a look at this answer, where you can find basic example with code implementation for server and client (javascript and java too):

How to send binary data with socket.io?

The good part is that it also works on Android! (if you wish)

Cheers

like image 36
chelo_c Avatar answered Sep 19 '22 00:09

chelo_c