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). - Resilient: Mutex poison-recovery and graceful error handling — malformed client messages are rejected without crashing the server.
Installation
Add this to your Cargo.toml:
[]
# For most users, 'server' is required to get the built-in sync server logic.
= { = "0.1.3", = ["server"] }
Feature Flags
server(Recommended): Enablesaxumintegration and provides built-in WebSocket handlers for synchronization.sqlite: Enablesrusqlitepersistence layer to store document updates.
Performance Profile and Intended Use
This crate is designed for high performance in low-concurrency environments.
- It is single-threaded by design for document persistence, because SQLite is single-threaded in this architecture.
- It is ideal for a handful of users collaborating across multiple devices (low concurrent load).
- It is especially well-suited for embedding collaborative sync into applications.
- It is currently used both as a standalone personal server deployment and embedded inside a Tauri desktop app.
If you need to support many users concurrently, prefer the hocuspocus-rs-ws crate: https://github.com/albireo3754/hocuspocus-rs-ws
Client Compatibility
This server implementation is designed to connect with the Hocuspocus JavaScript client and Dart/Flutter y-crdt clients.
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.