🎥

Notes on Group Video Chat with WebRTC and Simple Peer

Jul 27, 2024

Group Video Chat Using WebRTC and Simple Peer

Overview

  • Topic: Creating a group video chat using Simple Peer and WebRTC technology.
  • Importance of understanding the peer-to-peer nature of WebRTC.
  • Watch prior videos for foundational knowledge (links provided).

Problem with WebRTC

  • Each peer can only connect to one other peer directly.
  • To create a group video chat, a mesh network must be established.
  • In a mesh network:
    • Each participant has a separate peer connection to every other participant.
    • E.g., for 5 participants, 4 connections needed.

Setting Up the Project

  • Starter Files: Available through GitHub repository link.
  • Focus on:
    • Server JS file (back-end, including socket.io)
    • Room JS file (client-side group video chat logic).

Key Concepts

  • Socket.io:
    • Used for signaling and facilitating handshakes between peers.
    • Provides a way for the server to notify existing users when a new participant joins.
  • **Peer Connections:
    • Array of peers managed to maintain connections between participants.
    • Each peer has a unique socket ID.

Code Flow

Joining the Room

  • useEffect runs on joining a room:
    • Asks for media devices (audio + video).
    • Functions invoked:
      • createPeer
      • addPeer
  • Keep an array of current peers.

Creating Peers

  1. For each participant in the room, create a new peer connection:
    • Initiator set to true for the user joining.
    • Signals sent to existing peers notifying them of the new user's presence.
  2. Existing peers add the new user with addPeer method:
    • Initiator set to false.

Handling Connections

  • On user join, emit an event:
    • Server notifies all peers of the new user.
  • Each existing peer:
    • Calls addPeer to create a new peer connection.
    • Accepts incoming signals and sends back their own signal.

Peer Methods

Create Peer Method Logic:

  • Create a new peer object with the stream and initiator set to true.
  • Emit signal back to server for other peers.

Add Peer Method Logic:

  • Create new peer with initiator set to false and accept the incoming signal from the new member.
  • Emit a return signal back to the new user with their peer connection data.

Event Handling

  • Handling emitted events (e.g., signals) properly ensures a successful handshake between peers.
  • The returning signal needs to match the correct peer object for seamless connection.

Testing

  • Launch application on localhost (port 3000).
  • Open multiple tabs to initiate group video chats and verify connections.
    • Can join the same room by using the unique room ID.

Conclusion

  • Successful setup of group video chat with multiple participants.
  • Encouragement to like, subscribe, and look forward to future videos.