hocuspocus-rs
A Rust implementation of the Hocuspocus protocol (Yjs over WebSockets).
This crate provides a thread-safe handler for Yjs documents that follows the Hocuspocus V2 protocol, allowing Rust-based servers to synchronize with Hocuspocus and y-websocket clients.
Features
- Hocuspocus V2 Protocol: Full support for document-name prefixed messages.
- Yjs Sync: Seamless synchronization using the
yrslibrary. - Optional Persistence: Built-in SQLite persistence with debounced saving (via
sqlitefeature). - Awareness: Forwarding of awareness/presence messages.
- Axum Integration: Built-in WebSocket handlers for the Axum web framework (via
serverfeature).
Installation
Add this to your Cargo.toml:
[]
# For most users, 'server' is required to get the built-in sync server logic.
= { = "0.1.0", = ["server"] }
Feature Flags
server(Recommended): Enablesaxumintegration and provides built-in WebSocket handlers for synchronization.sqlite: Enablesrusqlitepersistence layer to store document updates.
Client Compatibility
This server implementation is designed to connect with the Hocuspocus JavaScript client.
In your frontend project, you can use the @hocuspocus/provider to connect:
import from '@hocuspocus/provider'
import * as Y from 'yjs'
const ydoc =
const provider =
Usage
Using with Axum
The server feature provides a create_router function that returns an axum::Router. You can mount this router at any path you choose.
use ;
use Arc;
use Router;
async
Configuration Details
- Endpoint: The built-in router handles
GET /sync(for multiplexed connections) andGET /sync/:room_name(for room-specific connections). You can change the base path by using.nest("/my-custom-path", hocuspocus_router)in your Axum setup. - Port: Control the port by changing the address passed to
TcpListener::bind. - Database: If the
sqlitefeature is enabled,AppState::new(db)accepts aDatabaseinstance. If disabled,AppState::new()takes no arguments.
Manual Integration (No Axum)
If you're using a different web framework, you can use the DocHandler directly:
use DocHandler;
async
Protocol Details
Hocuspocus V2 protocol prefixes every message with the document name as a VarString. This implementation handles that automatically, allowing multiple documents to be multiplexed over the same connection if needed.
License
BSD-3-Clause - Copyright (c) 2026, Jagtesh Chadha.