reactjswebrtcpeerjssimple-peer

how to establish connection between peers in peer js


My aim is connecting webrtc peers and sharing their video, but I'm not able to connect peers in the below code.

const [stream, setstream] = useState();
const myVideo = useRef();
const userVideo = useRef();

useEffect(() => {
    navigator.mediaDevices
        .getUserMedia({ video: true, audio: true })
        .then((currentStream) => {
            myVideo.current.srcObject = currentStream;
            setstream(currentStream);
        });
}, []);

var peer1 = new Peer({ initiator: true, stream: stream, wrtc: wrtc });
var peer2 = new Peer({ wrtc: wrtc });

peer1.on('signal', (data) => {
    peer2.signal(data);
});

peer2.on('stream', (stream) => {
    console.log('connected');
});

I know this will not work so how can I connect peers with id? Please help me in this problem.


Solution

  • The first example in the PeerJS API documentation shows how to connect two peers. I would recommend you start there; if you get stuck again, don't hesitate to update your question and I'll try assist you further.

    I would have written this in a comment, but I don't have enough reputation.