Skip to main content

WebSocket Protocol

SDD Classification: L3-Technical Authority: Engineering Team Review Cycle: Quarterly
This document defines the WebSocket protocol specification for Relay’s real-time collaboration service, including message formats, handshake flow, connection lifecycle, and error handling.

Protocol Overview


Connection Establishment

WebSocket Endpoint

Handshake Request

Handshake Response

Connection Parameters


Message Format

Binary Frame Structure

All messages use binary WebSocket frames with Protocol Buffer encoding:

Message Types

Protocol Buffer Definitions


Client-to-Server Messages

Operation Message

Sent when user performs an edit:

Presence Update

Sent when user presence changes:

Cursor Update

Sent on cursor movement:

Heartbeat

Sent every 30 seconds:

Sync Request

Request full document state:

Server-to-Client Messages

Operation Broadcast

Broadcast operation from another user:

Operation Acknowledgment

Confirm operation receipt:

Presence Broadcast

Broadcast presence changes:

Sync Response

Full document state:

Error Message


Connection Lifecycle

State Machine

Connection States

Reconnection Strategy


Heartbeat Protocol

Configuration

Heartbeat Flow

Implementation


Error Codes

WebSocket Close Codes

Error Response Format


Rate Limiting

Limits per Connection

Rate Limit Response


Compression

Message Compression

Large messages (>1KB) are compressed using LZ4:

Compression Thresholds


Security Considerations

Token Validation

  • JWT validated on every connection
  • Token refresh during long sessions
  • Connection closed on permission revocation

Message Validation

  • All operations validated against document schema
  • Input sanitized for XSS prevention
  • Position bounds checked against document length

Rate Limiting

  • Per-user and per-connection limits
  • Graduated throttling before disconnection
  • Abuse detection and blocking

Client Implementation Example



Document Status: Complete Version: 2.0