• Townie
    AI
  • Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
c15r

c15r

ParclandWebRtcServer

Public
Like
ParclandWebRtcServer
Home
Code
3
README.md
H
index.ts
test-client.html
Branches
1
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
Code
/
Search
index.ts
https://c15r--752c8bf437cd11f0b0e09e149126039e.web.val.run
README.md

Y-WebRTC Signaling Server

A WebSocket signaling server for y-webrtc collaborative editing. This server handles the WebRTC signaling process that enables peer-to-peer connections between clients using Yjs documents.

Features

  • Room-based signaling: Clients join named rooms for collaboration
  • Peer discovery: Automatic notification when peers join/leave rooms
  • Message routing: Forwards WebRTC signaling messages between peers
  • Auto cleanup: Removes inactive rooms and handles disconnections
  • Health monitoring: Built-in stats and health endpoints
  • Lightweight: Minimal dependencies, runs on Val Town

Usage

Client-side (JavaScript)

import * as Y from 'yjs' import { WebrtcProvider } from 'y-webrtc' // Create a Yjs document const ydoc = new Y.Doc() // Connect to the signaling server const provider = new WebrtcProvider('my-room-name', ydoc, { signaling: ['ws://your-val-town-url.web.val.run'] }) // Use shared types const ytext = ydoc.getText('shared-text') const ymap = ydoc.getMap('shared-map')

With React and Yjs

Create val
import { useEffect, useState } from 'react' import * as Y from 'yjs' import { WebrtcProvider } from 'y-webrtc' function CollaborativeEditor({ roomName }) { const [ydoc] = useState(() => new Y.Doc()) const [provider, setProvider] = useState(null) const [text, setText] = useState('') useEffect(() => { // Connect to signaling server const webrtcProvider = new WebrtcProvider(roomName, ydoc, { signaling: ['ws://your-val-town-url.web.val.run'] }) const ytext = ydoc.getText('content') // Listen for changes const updateText = () => setText(ytext.toString()) ytext.observe(updateText) updateText() setProvider(webrtcProvider) return () => { webrtcProvider.destroy() } }, [roomName, ydoc]) const handleChange = (newText) => { const ytext = ydoc.getText('content') ydoc.transact(() => { ytext.delete(0, ytext.length) ytext.insert(0, newText) }) } return ( <textarea value={text} onChange={(e) => handleChange(e.target.value)} placeholder="Start typing to collaborate..." /> ) }

API Endpoints

WebSocket Connection

  • URL: ws://your-val-town-url.web.val.run
  • Protocol: WebSocket upgrade from HTTP

HTTP Endpoints

GET /

Returns a web interface with server information and real-time statistics.

GET /stats

Returns JSON with current server statistics:

{ "totalRooms": 3, "totalClients": 7, "rooms": [ { "id": "room-1", "clients": 3, "lastActivity": "2024-01-15T10:30:00.000Z" } ] }

GET /health

Health check endpoint:

{ "status": "ok", "timestamp": "2024-01-15T10:30:00.000Z" }

WebSocket Message Protocol

The server expects JSON messages with the following structure:

Join Room

{ "type": "join", "room": "room-name", "peerId": "unique-peer-id" }

Leave Room

{ "type": "leave" }

Signal Message

{ "type": "signal", "data": { // WebRTC signaling data (offer, answer, ice candidates, etc.) } }

Server Responses

The server sends messages in the same format:

  • Peer joined: { "type": "signal", "peerId": "peer-id", "data": { "type": "peer-joined" } }
  • Peer left: { "type": "signal", "peerId": "peer-id", "data": { "type": "peer-left" } }
  • Room joined: { "type": "signal", "data": { "type": "room-joined", "peers": ["peer1", "peer2"] } }
  • Forwarded signals: { "type": "signal", "peerId": "sender-id", "data": {...} }

Configuration

The server includes several configurable parameters:

  • Room cleanup interval: 5 minutes (removes inactive rooms)
  • Room inactivity timeout: 30 minutes
  • Automatic ping/pong: Keeps connections alive

Deployment

This server is designed to run on Val Town and will automatically:

  1. Handle WebSocket upgrades
  2. Manage room lifecycles
  3. Route signaling messages
  4. Provide monitoring endpoints

Simply deploy to Val Town and use the provided URL as your signaling server in y-webrtc configuration.

Security Considerations

  • No authentication is implemented by default
  • Room names should be treated as public
  • Consider adding rate limiting for production use
  • WebRTC connections are peer-to-peer after signaling

Troubleshooting

Common Issues

  1. Connection fails: Ensure WebSocket URL is correct and accessible
  2. Peers not connecting: Check that both clients are in the same room
  3. Frequent disconnections: May indicate network issues or server restarts

Debug Mode

Monitor the server logs and use the /stats endpoint to see:

  • Active rooms and client counts
  • Last activity timestamps
  • Connection patterns

The web interface at / provides real-time statistics and connection information.

HTTP
  • index.ts
    c15r--75…9e.web.val.run
Code
README.md
H
index.ts
test-client.html
FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.