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

35,722

Solution 1

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

Solution 2

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

Share:
35,722
MaiaVictor
Author by

MaiaVictor

Updated on December 05, 2020

Comments

  • MaiaVictor
    MaiaVictor over 3 years

    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?