Jason
Jason

Reputation: 2743

How can I send binary data via WebSocket from Node.js to a web page?

I've tried sending a Float32Array and an ArrayBuffer a few different ways but the buffers I receive on the client side always seem empty. How should I send the data? How can I access it?

Here's my latest attempt:

server.js

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8000 });

wss.on('connection', function connection(ws) {

    var arr1 = new Float32Array(4);
    for (var i = 0; i < arr1.length; ++i)
        arr1[i] = (i + 100) / 7;

//    ws.send(arr1.buffer);


    var arr2 = new ArrayBuffer(8);
    for(var i = 0; i < 8; i++)
        arr2[i] = i * 17;

    ws.send(arr2);    
});

client.html

<!DOCTYPE html>
<html>
<head></head>
<body>

<script>

var gData = null;

var ws = new WebSocket("ws://192.168.0.7:8000");
ws.binaryType = 'arraybuffer';

ws.onmessage = function(message) {
    console.log(message.data);
    gData = message.data;
};

</script>

</body>
</html>

Console Output

ArrayBuffer(8) {}

Also, just because I'm curious, how can I send binary data back to the server?

Upvotes: 5

Views: 12754

Answers (2)

Derzu
Derzu

Reputation: 7146

You need to create the WebSocket and set its type to arraybuffer (binaryType = 'arraybuffer'). I think you forgot that on the server-side.

In this example, the bytes are obtained from a base64 string info.

    // https://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer
    function base64ToArrayBuffer(base64) {
        var binary_string = Buffer.from(base64, 'base64').toString('binary');
        var len = binary_string.length;
        var bytes = new Uint8Array(len);
        for (var i = 0; i < len; i++) {
            bytes[i] = binary_string.charCodeAt(i);
        }    

        return bytes.buffer;
    }

    // websocket server
    const WebSocket = require("ws"); // websocket server
    const wss = new WebSocket.Server({ port: 8082 });
    console.log("WebSocket Server Started on port 8082");

    wss.binaryType = 'arraybuffer';
    const content_base64 = "c3RyZWFtIGV2ZW50"; // Place your base64 content here.
    const binaryData = base64ToArrayBuffer(content_base64);

    wss.on("connection", (ws) => {
        console.log("WebSocket sending msg");
        ws.send(binaryData);
    });

Don't forget to install the npm websocket module through the command:

$ npm install websocket

Upvotes: 2

emax
emax

Reputation: 521

ArrayBuffer it's just a buffer, in order to access the memory contained in a buffer, you need to use a view:

var arr2 = new ArrayBuffer(8);
var view = new Uint8Array(arr2);
for(var i = 0; i < 8; i++)
    view[i] = i * 17;
ws.send(arr2);  

Upvotes: 1

Related Questions