rvighne
rvighne

Reputation: 21897

Data sent as ArrayBuffer is being recieved as Blob

I'm using the ws module for a WebSocket server in nodejs. On the server side, I have a Uint8Array that I send to the client using code very much like this,

var data = new Uint8Array([1, 2, 3, 4, 5]);
clientSock.send(data, {
    binary: true
});

However, on the client side (both the latest Chrome and Firefox), the data is recieved as a Blob object. I know I can process this back into a Uint8Array using the FileReader API. But I want to receive it as an ArrayBuffer in the first place. How can I do this?

Upvotes: 2

Views: 821

Answers (1)

rvighne
rvighne

Reputation: 21897

Just as I finished writing this question, I happened to find the answer on my own. According to the MDN documentation for WebSocket, there is a binaryType property that has to be set to either "blob" or "arraybuffer", and it determines the format in which the data is received. By changing the client-side code like so,

var sock = new WebSocket("<the server address>");
sock.binaryType = "arraybuffer";

Now all the binary data from the server is received as ArrayBuffers, as expected.

Upvotes: 7

Related Questions