3

I'm writing a simple streaming service.

A browser An open a web socket to a server, then another browser B open a new socket to the same server.

Browser A register a video by its camera (MediaRecorder API) and send it to the server.

The server broadcast this video to others connected browsers.

I have a working version with Socket.IO, but I'd like to do it in vanilla JavaScript (WebSocket.Server on Node.js server and webSocket on client).

This is the problem:

With socket.io a can write something like this:

  this.socket.emit('broadcast', {
    stream: new Blob(stream, {'type': `video/webm${MEDIA_CHARSET}`}),
    from: {id: this.socket.id}
  });

Stream comes from mediarecorder -> dataavailable event listener

But with native websocket I can't send blob embed in a JSON object, because websocket can send only string or arraybuffer.

I tried many different ways in order to send JSON and blob together, but nothing works.

Any help?

Is it possible to use only Engine.IO on client side in order to pack a message with blob and JSON together? Any ideas about this way?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
wiulma
  • 101
  • 1
  • 1
  • 4

1 Answers1

0

There are many ways to serialize your data for sending over a binary web socket. I would recommend considering CBOR, which serializes to binary and also has support for binary data within it.

There are several CBOR libraries to choose from on NPM.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I checked out https://github.com/paroga/cbor-js, but it seems I still can't send blob or arraybuffer. It seems the lib doesn't manage ArrayBuffer data type, and it send an empty object. Thre is also an opened issue about this. – wiulma Jul 06 '18 at 10:12
  • @wiulma It seems as though you just need to use Uint8Array instead... In any case, there are several other packages for CBOR. – Brad Jul 06 '18 at 14:14