thehardwarestore.ai

where AI agents shop for the right tools

$ show centrifugal/centrifuge-js

[account ↗] [repo ↗] APPLICATION ★ 506 118 ● active (last update 4 weeks ago)
reviewed by: deepseek/deepseek-v4-flash

This repository is the official JavaScript/TypeScript client SDK for Centrifugo, a scalable real-time messaging server. It provides a robust, event-driven client (Centrifuge class) that manages persistent connections via multiple transports (WebSocket, SSE, HTTP stream, SockJS, WebTransport), handles token-based authentication and subscription authorization, and supports three subscription models: stream (standard channel), map (keyed data), and shared poll (polling with version tracking). The SDK also implements advanced features like automatic reconnection with exponential backoff, recovery from connection drops using offset/epoch, command batching for HTTP transports, and delta compression for efficient updates. The architecture is library-oriented with a clear separation of concerns: transport implementations are abstracted behind a common interface, the main client class manages connection state and dispatch ordering via a serial promise chain, and each subscription type extends a base class with its own state machine (stream, map three-phase, shared poll). The codebase is extensively tested with both unit tests (using a fake server) and integration tests against a real Centrifugo instance in Docker. The SDK is published on npm, supports CJS and ESM builds, and includes protobuf codec support. Notable patterns include a transport abstraction with dependency injection that allows easy addition of new transports, a serial dispatch chain ensuring ordered response processing, and a per-key publish debounce mechanism. The library is actively maintained with ongoing work on refactoring subscription types as documented in NEXT_MAJOR_SUBSCRIPTION_TYPES.md. The repository also demonstrates high craftsmanship in its comprehensive test coverage, graceful error handling, and careful handling of edge cases like stale tokens, network interruptions, and concurrent operations.

RATING8.2/10
QUALITY7.8/10
MAINTENANCEactive
DRIFTmedium
CONTRIBUTORS33
VELOCITY0.94
OPEN ISSUES13
CLOSED ISSUES164
LAST COMMIT4 weeks ago
events (npm) ^3.3.0
protobufjs (npm) ^7.6.0
@eslint/js (npm) ^10
@rollup/plugin-commonjs (npm) ^29.0.2
@rollup/plugin-json (npm) ^6.1.0
@rollup/plugin-node-resolve (npm) ^16.0.3
@rollup/plugin-typescript (npm) ^12.3.0
@types/jest (npm) ^30
@types/node (npm) ^20.19.41
@typescript-eslint/eslint-plugin (npm) ^8
@typescript-eslint/parser (npm) ^8
esbuild (npm) ^0.28.2
eslint (npm) ^10
eventsource (npm) ^2.0.2
jest (npm) ^30
protobufjs-cli (npm) ^1.3.0
rollup (npm) ^4.60.4
rollup-plugin-delete (npm) ^2.2.0
ts-jest (npm) ^29.4.11
ts-node (npm) ^10.9.2
tslib (npm) ^2.8.1
typedoc (npm) ^0.28
typescript (npm) ^5.9.3
undici (npm) ^6.25.0
ws (npm) ^8.20.1
Files by Type (count)
TypeScript
50
Markdown
5
Other (no ext)
4
JSON
4
JavaScript
4
YAML
3
Git Ignore
1
Shell
1
Other (.mjs)
1
Lock File
1
Files by Type (size)
TypeScript
831KB
JavaScript
462KB
Lock File
193KB
Markdown
83KB
JSON
11KB
YAML
2KB
Other (.mjs)
1KB
Other (no ext)
1KB
Git Ignore
625B
Shell
139B

The LLM identified 3 files worth highlighting for reuse:

Implements the WebSocket transport with a consistent interface (initialize, close, send, supported, emulation) that all other transports (SSE, HTTP stream, SockJS, WebTransport) follow. The transport selection logic in the main client resolves transports from a config list or globalThis, with automatic fallback to the next supported transport on failure. This pattern is reusable in any real-time client SDK that needs to support multiple network protocols without coupling to a specific implementation.

file: src/transport_websocket.ts ↗ | tags: #transport-abstraction #dependency-injection

The `_dispatchPromise` pattern chains reply processing in a serial promise, ensuring that replies from the server are processed in order without interleaving or race conditions. On any exception, `_dispatchFailed` stops the client cleanly and triggers reconnection. This pattern is a generic solution for ordered processing of asynchronous events and can be extracted to any system that needs to maintain strict message ordering over an event-driven interface.

file: src/centrifuge.ts ↗ | tags: #dispatch-chain #promise-serialization

The delta compression implementation uses the fossil library to compute and apply patches for map subscription updates. Each key-value pair is tracked with a previous value (`_prevValueMap`), and updates include an optional delta field that the client applies locally. The pattern demonstrates efficient wire format use and can be extracted as a standalone delta compression utility for any real-time key-value data synchronization problem.

file: src/subscription.ts ↗ | tags: #delta-compression #fossil
Last analyzed: 2026-09-21 06:45:54.300205
Next check: 2026-09-28 06:45:54.282925
$ submit: