Skip to content

KartikeyNamdev/WebRTC_Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŽ₯ WebRTC Video Streaming Application

WebRTC TypeScript React WebSocket

Real-time peer-to-peer video streaming using WebRTC and WebSockets

Features β€’ Installation β€’ Usage β€’ Architecture β€’ Troubleshooting


✨ Features

πŸš€ Core Features

  • βœ… Real-time video streaming
  • βœ… Peer-to-peer connection via WebRTC
  • βœ… WebSocket signaling server
  • βœ… ICE candidate exchange
  • βœ… SDP offer/answer mechanism

πŸ› οΈ Technical Stack

  • βš›οΈ React 18+ with TypeScript
  • πŸ”Œ WebSocket (ws library)
  • πŸ“Ή WebRTC API
  • 🎨 Inline CSS styling
  • πŸ”’ STUN server integration

πŸ“¦ Installation

Prerequisites

Make sure you have the following installed:

  • Node.js (v16 or higher)
  • npm or yarn

Clone the Repository

git clone <your-repository-url>
cd webrtc-video-streaming

Install Dependencies

For the Frontend (React App)

npm install
# or
yarn install

For the WebSocket Server

cd server
npm install ws
# or
yarn add ws

πŸš€ Usage

Step 1: Start the WebSocket Server

cd server
npx tsx server.ts
# or if you have ts-node installed
ts-node server.ts

You should see:

WebSocket server running on ws://localhost:8080

Step 2: Start the React Application

In a new terminal:

npm run dev
# or
yarn dev

Step 3: Open Two Browser Windows

  1. Sender: Navigate to http://localhost:5173/sender
  2. Receiver: Navigate to http://localhost:5173/receiver

Step 4: Start Streaming

  1. On the Sender page, click "Send Video"
  2. Grant camera permissions when prompted
  3. The Receiver page will automatically start displaying the video stream

πŸ—οΈ Architecture

graph LR
    A[Sender] -->|WebSocket| B[Signaling Server]
    B -->|WebSocket| C[Receiver]
    A -.->|WebRTC P2P| C

    style A fill:#4CAF50
    style C fill:#2196F3
    style B fill:#FF9800
Loading

How It Works

  1. Connection Setup

    • Sender and Receiver connect to WebSocket server
    • Both identify themselves (identify-as-sender / identify-as-receiver)
  2. Signaling Process

    Sender β†’ Create Offer β†’ Server β†’ Receiver
    Receiver β†’ Create Answer β†’ Server β†’ Sender
    
  3. ICE Candidate Exchange

    • Both peers exchange ICE candidates through the server
    • Establishes optimal connection path
  4. Media Streaming

    • Direct peer-to-peer video transmission via WebRTC
    • No server involved in media transfer

πŸ“ Project Structure

webrtc-video-streaming/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ Sender.tsx       # Video sender component
β”‚   β”‚   └── Receiver.tsx     # Video receiver component
β”‚   β”œβ”€β”€ App.tsx              # Main application
β”‚   └── main.tsx             # Entry point
β”œβ”€β”€ server/
β”‚   └── server.ts            # WebSocket signaling server
β”œβ”€β”€ package.json
└── README.md

πŸ”§ Configuration

WebSocket Server

Port: 8080 (default)

To change the port, edit server.ts:

const wss = new WebSocketServer({ port: 8080 });

STUN Servers

Located in both Sender.tsx and Receiver.tsx:

const pc = new RTCPeerConnection({
  iceServers: [
    { urls: "stun:stun.l.google.com:19302" },
    { urls: "stun:stun1.l.google.com:19302" },
  ],
});

πŸ› Troubleshooting

Issue: Video Not Playing

Symptoms: Video shows "Connecting..." but never plays

Solutions:

  1. βœ… Ensure WebSocket server is running
  2. βœ… Check browser console for errors
  3. βœ… Grant camera permissions to the Sender page
  4. βœ… Make sure both Sender and Receiver are connected
  5. βœ… Try refreshing both pages

Issue: "WebSocket is not connected"

Solutions:

  1. Restart the WebSocket server
  2. Wait 2-3 seconds after page load before clicking "Send Video"
  3. Check if port 8080 is available

Issue: ICE Connection Failed

Solutions:

  1. Check your firewall settings
  2. Ensure STUN servers are accessible
  3. Try adding TURN servers for better connectivity

Common Error Messages

Error Cause Solution
TypeError: Failed to construct 'RTCIceCandidate' Invalid ICE candidate Update to latest server code
Camera access denied Browser permissions Allow camera access in browser settings
WebSocket closed Server not running Start the WebSocket server

πŸ” Debug Mode

To see detailed logs, open browser DevTools Console:

Sender logs to look for:

βœ“ Sender: WebSocket connected
βœ“ Sender: Creating offer...
βœ“ Sender: Offer sent to receiver
βœ“ Sender: Received answer from receiver
βœ“ Sender: Connection state: connected

Receiver logs to look for:

βœ“ Receiver: WebSocket connected
βœ“ Receiver: Processing offer
βœ“ Receiver: Answer sent to sender
βœ“ Receiver: Track received! video
βœ“ Receiver: Video playing!

🎯 Key Concepts

WebRTC (Web Real-Time Communication)

Enables peer-to-peer audio, video, and data sharing directly between browsers without intermediary servers.

Signaling

The process of coordinating communication and sending control messages between peers. Uses WebSocket in this project.

ICE (Interactive Connectivity Establishment)

Framework for connecting peers across NATs and firewalls using STUN/TURN servers.

SDP (Session Description Protocol)

Format for describing multimedia communication sessions (codecs, formats, etc.).


🚦 API Reference

WebSocket Messages

Sender β†’ Server

// Identify as sender
{ type: "identify-as-sender" }

// Send offer
{ type: "create-offer", sdp: RTCSessionDescription }

// Send ICE candidate
{ type: "ice-candidate", candidate: RTCIceCandidate }

Receiver β†’ Server

// Identify as receiver
{ type: "identify-as-receiver" }

// Send answer
{ type: "create-answer", sdp: RTCSessionDescription }

// Send ICE candidate
{ type: "ice-candidate", candidate: RTCIceCandidate }

πŸ“Š Performance Tips

  • 🎬 Video Quality: Modify constraints in getUserMedia()
  • πŸ”Œ Network: Use TURN servers for better NAT traversal
  • πŸ’Ύ Bandwidth: Adjust video bitrate in RTCPeerConnection
  • πŸ–₯️ CPU: Lower video resolution for better performance

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments

  • WebRTC API Documentation
  • MDN Web Docs
  • React Documentation
  • ws WebSocket library

Made with ❀️ and TypeScript

⭐ Star this repo if you find it helpful!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published