6

After looking to implement WebRTC with a Client to Server model (like Discord), I came to the conclusion that the way to do this is to have 2 clients - the server and client. Audio streams can be overlayed and sent back to the user in 1 single stream.

backend/server.js

const clientPeer = new Peer({ initiator: true, wrtc });
    
clientPeer.on('connect', () => console.log('hi client, this is server'));
clientPeer.on('data', (data) => console.log('got a message from client peer: ', data));

frontend/index.js

serverPeer.on('connect', () => console.log('Connected to server'));
serverPeer.on('stream', async (stream) => {
  const video = document.createElement('audio');

  ('srcObject' in video)
    ? video.srcObject = stream
    : video.src = window.URL.createObjectURL(stream);

  await video.play();
});

How would I implement sending media streams between the client and server?

ADAMJR
  • 1,880
  • 1
  • 14
  • 34
  • I do not fully understand what exactly you want to achieve, but I think SFU (for example: [mediasoup](https://mediasoup.org/)) could be relevant/required to it. – artur grzesiak Aug 25 '21 at 09:46

1 Answers1

4

A possible solution can be: Create a MediaRecorder object, which can record the media streams on the client-side. This object emits data chunks over time. You can send these chunks via WebSocket to the server. On the server side, you can do what you want with the data chunks. For more details, you can check this https://mux.com/blog/the-state-of-going-live-from-a-browser/.

https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder Another solution can be: Making a node.js application a PEER with WebRTC

Anarno
  • 1,470
  • 9
  • 18
  • I was considering sending data in chunks with a WebSocket, but am uncertain of the audio quality. This could be a viable solution if the audio quality was good. Do you know any good code examples of this? – ADAMJR Aug 25 '21 at 13:46
  • I'm not sure about the quality, because we used this solution as just a proof of concept. But if your connection with the WebSocket is ok and your machine with the media recorder is also ok then I think the quality also will be ok. Please read this blog post about this solution, I made an update on the answer. – Anarno Aug 26 '21 at 04:17