Skip to content
Advertisement

Request video during PeerJs ongoing live connection (stream)

I am new to PeerJs and recently starting developing an app for my school during this Covid pandemic.

I have been able to deploy code to NodeJs server with express and was able to establish connection between 2 users.

But the problem arises when video is turned off from the beginning of stream for both users and a user wants to initiate a video call.

What I need is, to send some kind of notification to user 2 that user 1 is requesting for video. So that user 2 will turn on video.

My existing code is:

JavaScript

Please help.

Advertisement

Answer

You can send messages back and forth directly using peer itself const dataConnection = peer.connect(id) will connect you to the remote peer, it returns a dataConnection class instance that you can later use with the send method of that class.

Just remember that you also want to setup listener on the other side to listen for this events, like “open” to know when the data channel is open: dataConnection.on(‘open’, and dataConnection.on(‘data…

You have a bug in your code above, I know you didn’t ask about it, it is hard to see and not always will manifest. The problem will occur when your originator sends a call before the destination has had time to receive the promise back with its local video/audio stream. The solution is to invert the order of the calls and to start by setting up the event handler for peer.on(“call”, … rather than by starting by waiting for a promise to return when we ask for the video stream. The failure mode will depend on how long does it take for your destination client to signal it wants and call to the originator plus how long it takes for the originator to respond versus how long it takes for the stream promise to return on the destination client. You can see a complete working example, where messages are also sent back and forth here.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement