javascriptwebrtcrtcpeerconnectionsignaling

Can i re-use webRTC Peer Connection object for Multi user chat?


I have used WebRTC for my chat application. My idea is whenever any user has joined in the chat application then a new RTCPeerConnection object will create. The chat room will allow only one-to-one communication.

Example: Suppose User1 , User2 and User3 has joined application. If user1 want to chat with user2 then a Room will be created between those users.

Here user1 has RTCPeerConnection and user2 has it's own RTCPeerConnection. Next webRTC negotiation (exchange offer, answer and ICE) will happen between them via signaling. Data can be shared through RTC Data channel.

If user1 want to connect with user3, then user1 has to leave the previous room and do the webRTC negotiation with user3 (means previous room delete and new room has to create).

Now my question is:

Suppose user1 and user2 is doing webRTC communication then

user 1 RTCPeerConnection.signalingstatechange = have-local-offer

user 2 RTCPeerConnection.signalingstatechange = have-remote-offer

After that i have closed the connection (means room deleted). Next user1 want to connect with user3.

In this case, can i re-use the User1's RTCPeerConnection to create the new offer for user3 ?

If Yes, What are the things i have to taken care during the deletion of a previous connection ? because the previous signalingstatechange is have-local-offer and have-remote-offer. Any help or suggestion?


Solution

  • You can't re-use a PeerConnection like that. If you want to create a mesh connection between the three users it will look like.

    user-1 <--> user-2
      ^              ^
      |              |
      ---> user-3 <-- 
    

    Each user will create two PeerConnections to have P2P connections with the other users.