WebSocket server for edrys-Lite to create a connection between peers and share video streams.
This project is a modified version of y-websocket-server by Kevin Jahns.
The WebSocket server provides two main functions:
- A backend for y-websocket to handle Yjs document collaboration
- A video streaming server to enable real-time video sharing between peers
npm install
This repository implements a server that handles both Yjs document collaboration and video streaming in a single process. (source code)
Start the server:
node dist/server.cjs --port 3210
The server exposes two WebSocket endpoints:
ws://hostname:port/
- For Yjs document collaborationws://hostname:port/stream
- For video streaming
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
const doc = new Y.Doc()
const wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-roomname', doc)
wsProvider.on('status', event => {
console.log(event.status) // logs "connected" or "disconnected"
})
// Connect to the streaming endpoint
const ws = new WebSocket('ws://localhost:1234/stream')
// For stream source (sender)
ws.onopen = () => {
// Register as a video source for a specific room
ws.send(JSON.stringify({
type: 'register-source',
roomId: 'my-room-id'
}))
// Send video frames (example)
function sendFrame(frameData) {
ws.send(JSON.stringify({
type: 'frame',
data: frameData
}))
}
}
// For stream viewer (receiver)
ws.onopen = () => {
// Join a specific room to receive video
ws.send(JSON.stringify({
type: 'join-room',
roomId: 'my-room-id'
}))
}
// Handle incoming messages
ws.onmessage = (event) => {
const data = JSON.parse(event.data)
if (data.type === 'source-available') {
console.log('A video source is available in this room')
} else if (data.type === 'frame') {
// Handle incoming video frame
displayFrame(data.data)
}
}
The streaming WebSocket endpoint uses a simple JSON-based protocol:
-
Register as a stream source
{ "type": "register-source", "roomId": "unique-room-identifier" }
-
Join a room to receive video
{ "type": "join-room", "roomId": "unique-room-identifier" }
-
Send a video frame
{ "type": "frame", "data": "base64-encoded-frame-data" }
-
Ping/pong for latency measurement
{ "type": "ping", "timestamp": 1620000000000 }
-
Client statistics
{ "type": "stats", "fps": 30, "bufferSize": 2, "dropped": 0 }
The MIT License © Kevin Jahns
Fork maintained by: edrys-labs