Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket.send(blob) in React Native

I am trying to send blob data with Websocket in React Native.

My React native and web code are equal as following:

var websocket = new WebSocket(this.state.wsURI);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };

function onClose(evt){
  console.log('disconnected');
}

function onOpen(evt) {
  var message = {
    action: 'start',
    'content-type': 'audio/wav',
  };

  websocket.send(JSON.stringify(message));
  websocket.send(blob);
  websocket.send(JSON.stringify({action: 'stop'}));
}

In the web application, blob data is well sent. However in the React Native App, the error message ( Unsupported dataType for Websocket.send ) occurs.

The console log of blob in react native is,

Blob {listeners: Object, isRNFetchBlobPolyfill: true, multipartBoundary: null, _ref: "/Users/somepath/...-4454330B7F04/Documents/audio.wav", _blobCreated: true…}

Is there a way to appropriately send blob data using Websocket?

like image 559
Sungpah Lee Avatar asked Jun 26 '17 09:06

Sungpah Lee


1 Answers

Sadly React Native doesn't using support binary data in js side. See this issue https://github.com/facebook/react-native/issues/1424.

You can try to encode blob to base64 string which is less efficient but still better than nothing.

like image 146
Andzej Maciusovic Avatar answered Sep 30 '22 20:09

Andzej Maciusovic