@digo-labs/ws 0.2.0 → 0.2.1
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 +47 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,11 +1,54 @@
|
|
|
1
1
|
# @digo-labs/ws
|
|
2
2
|
|
|
3
|
-
The low-level websocket client (`WebsocketClient`), server
|
|
3
|
+
The low-level websocket client (`WebsocketClient`), the hub server (`@digo-labs/ws/server`), and the `digo-ws-hub` CLI for running a hub locally. Most apps use the typed `Websockets` wrapper from [`@digo-labs/services`](https://design.digo-labs.com/docs/guide/package-services) instead, which builds a `WebsocketClient` for you and adds the app name and auth token.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
The Digo design-system guide is the single source of truth for this package — its full API, examples, and patterns live there. This README stays thin on purpose and points to it.
|
|
5
|
+
**How to read this.** This guide ships with the package, so it matches the version you have installed; the site describes the latest release. The types in `node_modules/@digo-labs/ws/dist/*.d.ts` are exact and win if they disagree with it. Deeper pages: https://design.digo-labs.com/docs/guide/package-ws.
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
8
|
npm install @digo-labs/ws
|
|
11
9
|
```
|
|
10
|
+
|
|
11
|
+
## Client
|
|
12
|
+
|
|
13
|
+
`new WebsocketClient(config)` connects straight away. Every config field is optional: `url` (default `WS_URL` from `@digo-labs/common`, `wss://ws.digo-labs.com`), `app` (default `'unknown'`), `id` (default a random UUID), `reconnect` (default `true`), `reconnectInterval` / `maxReconnectInterval` (default `1000` / `10000` ms, doubled with jitter) and `getToken` (an async function returning a realtime token; without it the client identifies anonymously).
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { WebsocketClient } from '@digo-labs/ws';
|
|
17
|
+
|
|
18
|
+
const client = new WebsocketClient({ url: 'ws://localhost:9900', app: 'my-app' });
|
|
19
|
+
|
|
20
|
+
client.subscribe('scores', data => console.log(data));
|
|
21
|
+
client.send('scores', { team: 'red', points: 3 }); // one-off message to subscribers
|
|
22
|
+
client.setState('scores', { red: 3, blue: 1 }); // stored by the hub, sent to anyone who subscribes later
|
|
23
|
+
client.onReady(() => console.log('identified'));
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- `send` and `setState` called while the socket is open but before the hub confirms the client are queued and flushed once it does. Subscriptions are re-sent after every reconnect.
|
|
27
|
+
- `isLive(channel)` is `true` once the client is identified, subscribed and not denied on that channel.
|
|
28
|
+
- `unsubscribe(channel, listener?)` removes one listener, or the whole channel when no listener is given.
|
|
29
|
+
- Lifecycle: `onOpen`, `onReady` / `offReady`, `onClose`, `onError`. `close()` stops reconnecting for good.
|
|
30
|
+
|
|
31
|
+
## Server
|
|
32
|
+
|
|
33
|
+
`@digo-labs/ws/server` exports `WebsocketHub`, the hub itself (Node only):
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { WebsocketHub } from '@digo-labs/ws/server';
|
|
37
|
+
|
|
38
|
+
const hub = new WebsocketHub({ port: 9900, tokenSecret: process.env.WS_TOKEN_SECRET });
|
|
39
|
+
|
|
40
|
+
hub.onMessage((channel, data) => console.log(channel, data));
|
|
41
|
+
hub.getState('scores');
|
|
42
|
+
hub.close();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
It also answers `GET /health` with `{ ok, clients }`. `tokenSecret` lets it verify the tokens clients send through `getToken`.
|
|
46
|
+
|
|
47
|
+
## Running the hub locally
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npx digo-ws-hub # port 9900
|
|
51
|
+
npx digo-ws-hub 9901 # another port
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The CLI reads `WS_TOKEN_SECRET` from the environment and shuts down cleanly on Ctrl+C. Inside the core monorepo, `npm run ws` (or `npm run dev`) starts the same hub from source.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digo-labs/ws",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/client.js",
|
|
6
6
|
"module": "./dist/client.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"lint:fix": "eslint ./src --fix"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@digo-labs/common": "^0.2.
|
|
42
|
+
"@digo-labs/common": "^0.2.1"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"ws": "^8.18.0"
|