Real-Time Camera Streaming over WebSockets
A lightweight experimental project that streams live camera data from the browser to a Node.js backend using the WebSocket protocol. The backend saves the streamed data as a video file.
This project was built purely for learning and analysis purposes — to understand how real-time streaming can be implemented using WebSockets. While this is not the most efficient method for video streaming, it provides great insight into how data packets travel between a browser and a server in real-time.
The app allows you to:
- Open your camera directly from the browser.
- Start recording video.
- Stream the video data live to the server through a WebSocket connection.
- The server writes the received video chunks to a file using Node’s
fs.createWriteStream.
Once recording stops, the WebSocket connection closes and the video is finalized on the server.
-
Prepare Stream When the user clicks Record, the client sends a GET request to the
/prepareendpoint. The server:- Generates a unique stream ID.
- Creates a write stream using
fs.createWriteStream(). - Stores this stream in a shared in-memory map (like a
HashMapor in future, Redis). - Responds to the client with the generated stream ID.
-
Connect WebSocket The client receives the stream ID and connects to:
ws://localhost:8080?key=<streamId> -
Validate Connection The server validates the
key(stream ID).- If valid → starts listening for binary data (video chunks).
- If invalid → closes the socket immediately.
-
Stream Data The browser uses the MediaRecorder API to record camera frames. Each recorded video chunk (
Blob) is sent through the WebSocket to the server. The server writes these chunks to the file stream corresponding to thatkey. -
End Stream When recording stops:
- The client closes the WebSocket connection.
- The server detects the closure and ends the file write stream.
- ✅ Concurrent streaming — each client has an isolated session and file
- 🔄 Real-time streaming of video data via WebSocket
- 🎥 Record and stream camera footage directly from your browser
- 💾 Saves video streams as
.webmfiles on the server - 🧩 Simple client–server architecture for easy learning
- 🧠 Demonstrates how binary data travels over WebSockets
- ⚙️ Built entirely with native browser APIs and Node.js — no third-party media libraries
- React + Vite
- MediaRecorder API (for recording camera data)
- Native WebSocket API (for live streaming)
- Node.js Express
- ws (WebSocket library)
- fs (for writing streams to files)
While this works, using WebSockets for video streaming is not efficient for production. WebSockets weren’t designed for high-performance real-time media transfer — especially video/audio frames.
If you plan to build production-grade real-time streaming, look into:
- 🎞 WebRTC — Peer-to-peer, designed for real-time video/audio streaming
- ⚡ WebTransport — Successor to WebSockets, supports bidirectional low-latency streams over QUIC
- 🧮 WebCodecs API — Gives direct access to encoded media frames for more efficient handling
These technologies are optimized for latency, performance, and synchronization, unlike WebSockets.
git clone https://github.com/rpaudel379/realtime-camera-streaming-websocket.git
cd websocket-camera-streamerFor both client and server folders:
npm install
# or
bun installcd server
npm run dev
# or
bun run devcd client
npm run dev
# or
bun run devVisit:
http://localhost:5173
Then:
- Click Open Camera.
- Click Record — this triggers
/prepare, opens a WebSocket, and starts streaming. - Stop recording to finalize the video on the backend.
- Store video data in AWS S3 or cloud storage.
- Replace in-memory session map with Redis or database for distributed scalability.
- Use WebRTC or WebTransport for better streaming performance.
- Implement authentication and access control for streams.
- Add a simple playback UI to preview recorded videos.
- Enable file naming, progress indicators.
Contributions are welcome! If you’d like to improve this project (optimize streaming, refactor architecture, or add new features):
-
Fork the repo
-
Create a new branch
git checkout -b feature/amazing-improvement
-
Commit your changes
-
Push to your fork
-
Open a Pull Request 🎉
This project is not meant to be a production-grade streaming solution. It’s a learning experiment to:
- Explore the WebSocket protocol for binary data transfer.
- Understand media handling in the browser.
- Bridge frontend–backend communication for real-time applications.
Feel free to use it, modify it, and learn from it.
Built for learning. Inspired by curiosity.