RobinBobin
RobinBobin

Reputation: 565

React Native 0.60.5 - receive binary data over WebSocket

I open a WebSocket connection from my RN 0.60.5 app to my Node.js server and send binary data just fine (the app sends, the server receives. The server uses ws). But when I send binary data from the server, my app receives an empty ArrayBuffer. I believe my server code is right, because when I connect from my browser I receive binary data just fine.

As far as I remember, previously it was not possible even to send binary data from an RN app over a WebSocket. Am I missing something or binary transfers are supported only partially for now?

Thanks.

Upvotes: 1

Views: 1110

Answers (1)

Max Turner
Max Turner

Reputation: 156

I ran into the exact same problem and found that I needed to set the binaryType of the websocket object to 'blob'

var websocket = new WebSocket("ws://10.10.10.1/stream")
websocket.binaryType = 'blob';

websocket.onmessage = (event) => { ... } 

After that then I was able to pass the event.data into a FileReader to use the data, for my use case anyway.

Upvotes: 2

Related Questions