2

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?

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.