@arena-im/buzz-client 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # buzz-client
2
+
3
+ A TypeScript WebSocket client for Buzz applications. Works in Node.js and modern browsers. Ships ESM, CJS, and UMD bundles.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i
9
+ npm run build
10
+ ```
11
+
12
+ Bundles are output to `dist/`:
13
+
14
+ - `dist/buzz-client.esm(.min).js` (ESM)
15
+ - `dist/buzz-client.cjs(.min).js` (CommonJS)
16
+ - `dist/buzz-client.umd(.min).js` (UMD global `window.BuzzClient`)
17
+
18
+ ## Quick start
19
+
20
+ ### Node / Bundlers (ESM or CJS)
21
+
22
+ ```ts
23
+ import { BuzzClient } from "buzz-client"; // if published/installed
24
+ // or from source: import { BuzzClient } from "./dist/buzz-client.esm.js";
25
+
26
+ const client = new BuzzClient(
27
+ "prod", // env ("prod" | "dev") or a custom websocket URL
28
+ "chat", // namespace
29
+ "<site-id>", // siteId
30
+ null, // id token (JWT) or null for anonymous
31
+ { commandTimeout: 15000 }
32
+ );
33
+
34
+ const topic = client.topic("room:<room-id>");
35
+ const meta = await topic.emit("room_meta:get");
36
+ await topic.on("added", (evt) => console.log("New message", evt));
37
+ ```
38
+
39
+ ### Browser (UMD)
40
+
41
+ ```html
42
+ <script src="/dist/buzz-client.umd.js"></script>
43
+ <script>
44
+ const { BuzzClient } = window.BuzzClient;
45
+ const client = new BuzzClient("prod", "chat", "<site-id>", null, {
46
+ commandTimeout: 15000,
47
+ });
48
+ const t = client.topic("room:<room-id>");
49
+ t.emit("room_meta:get").then(console.log);
50
+ t.on("added", (evt) => console.log("added", evt));
51
+ </script>
52
+ ```
53
+
54
+ See runnable pages in `examples/browser-test.html`, `examples/chat/browser.html` and `examples/comments/browser.html`.
55
+
56
+ ## Examples
57
+
58
+ - `npm run example:chat`
59
+ - `npm run example:comments`
60
+
61
+ These scripts use `tsx` to run the examples in `examples/`. You will be prompted for required environment variables if not present:
62
+
63
+ - `ARENA_SITE_ID`
64
+ - `ARENA_TOKEN` (optional ID token; leave empty for anonymous)
65
+ - Chat: `ARENA_CHAT_ROOM_ID`
66
+ - Comments: `ARENA_SITE_SLUG`, `ARENA_PAGE_ID`
67
+
68
+ ## API
69
+
70
+ ### Constructor
71
+
72
+ ```ts
73
+ new BuzzClient(
74
+ envOrWebsocketUrl: "prod" | "dev" | string,
75
+ namespace: string,
76
+ siteId: string,
77
+ idToken: string | null,
78
+ options?: {
79
+ commandTimeout?: number;
80
+ websocket?: { headers?: Record<string, string> };
81
+ identityUrl?: string; // override identity service when using custom ws
82
+ }
83
+ )
84
+ ```
85
+
86
+ - When `idToken` is provided, the client exchanges it for a Buzz token.
87
+ - When `idToken` is `null`, the client obtains an anonymous token and then exchanges it.
88
+
89
+ ### Core methods
90
+
91
+ - `topic(name: string): Topic` — helper to work with a specific topic.
92
+ - `subscribe(topic: string): Promise<void>`
93
+ - `unsubscribe(topic: string): Promise<void>`
94
+ - `sendCommand<T>(type: string, topic: string, content?: Record<string, unknown>): Promise<CommandResponse<T>>`
95
+ - `onEvent(topic: string, eventName: string, cb: (event: unknown) => void): void`
96
+ - `clearEventListeners(): void`
97
+ - `disconnect(): Promise<void>` — close connection and clear internal state.
98
+ - `setToken(token: string | null): Promise<void>` — set/refresh the ID token (e.g., after login/logout). This triggers a reconnection.
99
+
100
+ ### Topic API
101
+
102
+ ```ts
103
+ class Topic {
104
+ emit<T>(
105
+ eventName: string,
106
+ content?: Record<string, unknown>
107
+ ): Promise<EmitResult<T>>;
108
+ on<T>(eventName: string, callback: (event: T) => void): Promise<void>;
109
+ }
110
+ ```
111
+
112
+ `emit` automatically ensures the topic is subscribed before sending.
113
+
114
+ ### Errors
115
+
116
+ - `CommandTimeoutError` — response not received in time.
117
+ - `CommandErrorResponse` — server rejected the command. Has `errorType: 'rejected' | 'error' | 'blocked'` and `message`.
118
+ - `WebsocketError` — underlying WebSocket problem. The client auto-reconnects, but in-flight commands are not retried.
119
+
120
+ > Note: When the connection errors, the Buzz client will automatically attempt to reconnect. However, **commands are not retried** after a reconnection — it's up to the caller to handle command retries if needed.
121
+
122
+ ### Throttling
123
+
124
+ Outgoing commands are rate-limited by an internal queue to align with server-side throttling. Defaults: up to 25 requests every 5s per connection. When the window is saturated, the queue waits for the next window.
125
+
126
+ ## Configuration
127
+
128
+ ### Runtime options
129
+
130
+ - `options.commandTimeout` — default 10s.
131
+ - `options.websocket.headers` — HTTP headers for Node.js `ws` constructor.
132
+ - `options.identityUrl` — override identity service base URL (useful with a custom websocket URL).
133
+
134
+ ### Build-time environment overrides
135
+
136
+ The build injects environment variables (via `rollup` + `@rollup/plugin-replace`) so you can change default endpoints at build time:
137
+
138
+ - `BUZZ_WEBSOCKET_URL`, `BUZZ_WEBSOCKET_URL_DEV`
139
+ - `IDENTITY_SERVICE_URL`, `IDENTITY_SERVICE_URL_DEV`
140
+
141
+ Create a `.env` before `npm run build` to override.
142
+
143
+ ## Development
144
+
145
+ ```bash
146
+ npm i
147
+ npm run build
148
+ # optional: watch
149
+ npm run build:watch
150
+ ```
151
+
152
+ Open the HTML examples under `examples/` from a local static server. Identity endpoints may enforce CORS; test from an allowed origin or proxy if needed.
153
+
154
+ ## Files of interest
155
+
156
+ - Library entry: `src/lib/index.ts`
157
+ - Token management: `src/lib/token-manager.ts`
158
+ - Reconnect logic: `src/lib/fault_tolerant_websocket.ts`
159
+ - Throttle queue: `src/lib/throttled_queue.ts`
160
+ - Examples: `examples/`