Sunday, 22 August 2021

Node.js - How to Implement Star Topology WebRTC (Client to Server) with SimplePeer?

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 a star topology with SimplePeer to send media streams between the client and server?



from Node.js - How to Implement Star Topology WebRTC (Client to Server) with SimplePeer?

No comments:

Post a Comment