Skip to content

MahmoudAboelazm/webrtc-peer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

WebRTC Peer

Basic setup for peer-to-peer connection between two browsers, supporting both data channels and media streams.

Setup

Make a clone of the repo for your project.

Usage

There are two setups one for the initiator peer and the other for the remote peer.

Initiator

const config = { initiator: true, stream };
const peer = new Peer(config);
peer.onSignal((signal) => {
  console.log(signal);
  // Send your signal to the remote peer by somehow
});

When the remote peer sends his signal back you should update your signal.

peer.setSignal(signal);

Remote

const config = { initiator: false, stream };
const peer = new Peer(config);
await peer.setSignal(initiatorSignal);
peer.onSignal((signal) => {
  console.log(signal);
  // Send your signal to the initiator peer by somehow
});

How to get the stream?

peer.onStream((stream) => {
  console.log(stream);
  // Render the stream in video or audio HTML element
});

Messages & Events

// For emitting messages and events
peer.emit("Event-or-Msg");

// To listen for all messages and events.
peer.onMessage((msg) => {
  console.log(msg);
});

// To listen for a special event
peer.on("event", () => {
  // Do something here
});

About

πŸš€ WebRTC Peer example ready to use

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published