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?

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.

Comments

Popular posts from this blog

Storage in React Native

React Native Duplicate resources

Implementation of a function object “power” operator in Raku