Skip to main content

WebSocket API Overview

SDD Classification: L3-Technical Authority: Engineering Team Review Cycle: Quarterly
Materi’s WebSocket API enables real-time collaboration features including live editing, presence awareness, instant comments, and synchronized document state. This guide covers the architecture, capabilities, and integration patterns.

Real-Time Collaboration Architecture


Key Features

FeatureDescription
Operational TransformConflict-free concurrent editing with automatic conflict resolution
Presence AwarenessReal-time user cursors, selections, and activity indicators
Instant CommentsLive comment threads and discussions
Document SyncAutomatic state synchronization across all connected clients
Connection RecoveryResilient connection handling with automatic reconnection

Connection URLs

Production Environment

PropertyValue
WebSocket URLwss://api.materi.dev/v1/ws
Document URLwss://api.materi.dev/v1/ws/documents/{document_id}
RegionGlobal with edge routing
ProtocolWSS (TLS 1.3 required)

Staging Environment

PropertyValue
WebSocket URLwss://api-staging.materi.dev/v1/ws
Document URLwss://api-staging.materi.dev/v1/ws/documents/{document_id}
PurposeTesting and development

Connection URL Format

wss://api.materi.dev/v1/ws/documents/{document_id}?token={jwt_token}&client_id={unique_client_id}

Query Parameters

ParameterRequiredDescription
tokenYesJWT access token for authentication
client_idYesUnique identifier for this client instance
versionNoProtocol version (default: 1)

Protocol Overview

Message Types

All WebSocket communication uses JSON messages with a standard structure:
TypeDirectionDescription
operationBidirectionalDocument edit operations
presenceBidirectionalUser presence and cursor updates
commentBidirectionalComment creation and updates
systemServer→ClientSystem notifications and state sync

Message Structure

{
  "type": "operation",
  "event": "document_edit",
  "data": {
    "operation": { ... },
    "version": 15,
    "timestamp": "2025-01-07T10:30:00Z"
  },
  "client_id": "client_abc123",
  "message_id": "msg_1234567890"
}

Connection Lifecycle

Connection States

StateDescription
ConnectingEstablishing WebSocket and authenticating
ConnectedFull real-time functionality available
DisconnectedTemporary loss, preparing to reconnect
ReconnectingActively trying to restore connection
ClosingIntentionally closing connection
FailedConnection permanently failed

Rate Limits

Connection Limits

ResourceFreeProfessionalEnterprise
Concurrent connections25Custom
Per-document connections510Custom
Operations/second1050Custom

Operation Throttling

Operations exceeding rate limits receive throttling responses:
{
  "type": "system",
  "event": "rate_limited",
  "data": {
    "retry_after": 1000,
    "message": "Operation rate limit exceeded"
  }
}

Security

Authentication

  • JWT token required for connection establishment
  • Token validated on connect and periodically during session
  • Automatic disconnection on token expiry
  • Permission validation for every operation

Encryption

  • WSS (TLS 1.3) required for all connections
  • No plain WebSocket (ws://) connections accepted
  • Certificate pinning supported for mobile clients

SDKs and Libraries

Official SDKs

JavaScript/TypeScript
npm install @materi/realtime
import { MateriRealtime } from '@materi/realtime';

const realtime = new MateriRealtime({
  accessToken: 'your_token',
  documentId: 'doc_123',
});

realtime.on('operation', (op) => {
  console.log('Received operation:', op);
});

await realtime.connect();
Python
pip install materi-realtime
from materi_realtime import MateriRealtime

realtime = MateriRealtime(
    access_token='your_token',
    document_id='doc_123'
)

@realtime.on('operation')
def handle_operation(op):
    print('Received operation:', op)

await realtime.connect()

Health and Diagnostics

Connection Health Check

// Check connection health
realtime.on('heartbeat', (latency) => {
  console.log(`Connection latency: ${latency}ms`);
});

// Check connection state
console.log(realtime.connectionState); // 'connected' | 'disconnected' | etc.

Debugging

Enable debug logging:
const realtime = new MateriRealtime({
  accessToken: 'your_token',
  documentId: 'doc_123',
  debug: true, // Enable debug logging
});


Document Status: Complete Version: 2.0