Conat Messaging System
Maintenance note: Update this file when conat protocol, subjects, DKV, pub/sub patterns, or service framework changes.
Package: packages/conat
Conat is CoCalc's custom distributed messaging system, inspired by NATS. It provides pub/sub, request/response, distributed key-value storage, TCP-like sockets, and a service framework across all components (frontend, hub, project).
Core Protocol
Location: packages/conat/core/
core/client.ts— MainClientclass (extends EventEmitter)core/server.ts— Server-side conat brokercore/types.ts— Protocol type definitionscore/patterns.ts— Subject pattern matching (NATS-style*and>wildcards)core/constants.ts— Protocol limits
Features
NATS-like pub/sub/request/response messaging
Automatic message chunking for large payloads (no size limits)
Multiple encoding formats: MsgPack (default, handles dates/buffers), JSON
Delivery confirmation and interest-based messaging
Hierarchical subject routing with wildcard patterns
Subscriptions survive client/server disconnects and reconnects
Socket.io transport (WebSocket mode)
Protocol Limits
Core Client Methods
Subject Naming
Location: packages/conat/names.ts
Messages are routed via hierarchical subjects. Use * for single-level wildcard and > for all remaining levels.
Account-scoped
Project-scoped
Hub-scoped
Public
DKV (Distributed Key-Value Store)
Location: packages/conat/sync/dkv.ts
An eventually consistent distributed key-value store for synchronized state across browsers, projects, and hubs. Built on CoreStream (persistent storage backend).
Key Properties
Multimaster: Any client can write; conflicts resolved via merge functions
Synchronous API: get/set/delete are sync; background save is automatic
3-way merge: Custom merge function
({key, local, remote, prev}) => resolved_valueDefault conflict strategy: Last write wins (local value)
Change events:
dkv.on('change', (key) => {...})Tombstone support: Deleted keys use TTL-based cleanup
Usage from Frontend
Usage from Project
Usage from Hub/Server
PubSub
Location: packages/conat/sync/pubsub.ts
Ephemeral publish/subscribe for transient data (not persisted). Common uses: cursor positions, active user presence, real-time indicators.
Service Framework
Location: packages/conat/service/
Request/reply microservice pattern for long-running services:
Socket Abstraction
Location: packages/conat/socket/
TCP-like sockets emulated on top of pub/sub. Provides in-order, reliable, lossless transmission with heartbeats, load balancing, and header support.
Hub API Layer
Location: packages/conat/hub/api/
Defines typed API interfaces that map function calls to conat subjects.
API Modules
| File | Service | Methods |
|---|---|---|
hub/api/projects.ts | Project management | createProject, start, stop, setQuotas, etc. |
hub/api/db.ts | Database operations | Query, update, delete |
hub/api/purchases.ts | Billing/purchases | Payment processing, subscriptions |
hub/api/jupyter.ts | Jupyter operations | Kernel management, execution |
hub/api/system.ts | System operations | Health, version, stats |
hub/api/messages.ts | User messaging | Send, receive, list |
hub/api/org.ts | Organizations | Org management |
hub/api/sync.ts | Sync operations | Collaborative editing sync |
Registration Pattern
Each API module exports a map of method names to auth wrappers:
Permission Levels
noAuth— Public, no authentication neededauthFirst— Requires sign-in if available, not mandatoryauthFirstRequireAccount— Requires signed-in user accountrequireAccount— Backend-only, must have account context
Implementations live in packages/server/conat/api/ (see hub.md).
Project API Layer
Location: packages/conat/project/api/
Defines the API that projects expose via conat:
| File | Service | Methods |
|---|---|---|
project/api/system.ts | System ops | ping, exec, signal, jupyterExecute, listing, deleteFiles, moveFiles, readTextFileFromProject, writeTextFileToProject |
project/api/editor.ts | Editor ops | File editing operations |
project/api/sync.ts | Sync ops | Real-time sync coordination |
Other Sync Primitives
Location: packages/conat/sync/
| Primitive | File | Purpose |
|---|---|---|
| DKV | sync/dkv.ts | Distributed key-value store |
| DKO | sync/dko.ts | Distributed key-value with ordered keys |
| PubSub | sync/pubsub.ts | Publish/subscribe (ephemeral) |
| AKV | sync/akv.ts | Append-only key-value |
| DStream | sync/dstream.ts | Distributed stream |
| AStream | sync/astream.ts | Append-only stream |
| SyncTable | sync/synctable.ts | Table-like sync structure |
| SyncTableKV | sync/synctable-kv.ts | Key-value synctable |
| CoreStream | sync/core-stream.ts | Low-level stream primitive |
Persistence
Location: packages/conat/persist/
Conat uses SQLite-backed persistent streams (not NATS JetStream):
persist/server.ts— Persistence serverpersist/storage.ts— SQLite storage backendpersist/auth.ts— Persistence authpersist/context.ts— Context management
Storage Properties
Per-project/account isolation (separate SQLite files)
Automatic compression
TTL-based cleanup for tombstones
Memory-efficient (SQLite handles data on disk)