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

MaiaVictor picture MaiaVictor · Jun 6, 2014 · Viewed 34.8k times · Source

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?

Answer

Edwin Dalorzo picture Edwin Dalorzo · Jun 6, 2014

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