@authup/core-realtime-kit 1.0.0-beta.69 โ 1.0.0-beta.71
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 +93 -11
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,22 +1,104 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/authup/authup/master/.github/assets/logo.svg" alt="Authup logo" width="96" height="96" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
[](https://github.com/authup/authup/actions/workflows/main.yml)
|
|
5
|
-
[](https://snyk.io/test/github/authup/authup)
|
|
5
|
+
<h1 align="center">@authup/core-realtime-kit</h1>
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<p align="center">
|
|
8
|
+
<b>A socket.io client manager and event-name helpers for Authup's realtime entity events.</b><br>
|
|
9
|
+
One manager per server: sockets per namespace, the access token sent on every handshake.
|
|
10
|
+
</p>
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
<p align="center">
|
|
13
|
+
<a href="https://www.npmjs.com/package/@authup/core-realtime-kit"><img src="https://img.shields.io/npm/v/@authup/core-realtime-kit" alt="npm version" /></a>
|
|
14
|
+
<a href="https://github.com/authup/authup/actions/workflows/main.yml"><img src="https://github.com/authup/authup/actions/workflows/main.yml/badge.svg" alt="CI" /></a>
|
|
15
|
+
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-blue" alt="License: Apache-2.0" /></a>
|
|
16
|
+
</p>
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="#installation"><b>Installation</b></a> ยท <a href="#usage">Usage</a> ยท <a href="#api">API</a> ยท <a href="https://authup.org">Documentation</a>
|
|
20
|
+
</p>
|
|
13
21
|
|
|
14
|
-
|
|
22
|
+
---
|
|
15
23
|
|
|
16
|
-
|
|
24
|
+
## Where it fits
|
|
25
|
+
|
|
26
|
+
When an entity changes, Authup's server publishes the change through `@authup/server-kit`'s socket handler, which emits it via a socket.io Redis emitter under a name such as `userCreated`. This package holds the two halves both sides agree on: `buildEventFullName`, which builds those names, and `ClientManager`, which a browser client uses to connect and listen. `@authup/server-kit` and [`@authup/client-web-kit`](https://github.com/authup/authup/tree/master/packages/client-web-kit) depend on it. Authup itself runs no socket.io server: the events reach a socket.io server you operate on the same Redis. Install this package directly only when you build such a client yourself.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```shell
|
|
31
|
+
npm install @authup/core-realtime-kit socket.io-client
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`socket.io-client` (`^4.8.4`) is a peer dependency: you install it. ESM only. Node.js `^22.13.0 || ^23.5.0 || >=24.0.0`, or a browser bundler.
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### Connect
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { ClientManager } from '@authup/core-realtime-kit';
|
|
42
|
+
|
|
43
|
+
const manager = new ClientManager({
|
|
44
|
+
url: 'https://realtime.example.com',
|
|
45
|
+
token: () => localStorage.getItem('access_token') ?? undefined,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const socket = await manager.connect('/');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
- `connect(namespace)` resolves once the socket is connected, or rejects with the `connect_error`. The default namespace is `/`.
|
|
52
|
+
- `token` is a string or a sync or async function. It is called on every handshake and sent as `auth.token`; a function that throws sends no token.
|
|
53
|
+
- The underlying socket.io `Manager` is created with `autoConnect: false`; nothing connects until you call `connect`.
|
|
54
|
+
- A path in `url` becomes the socket.io `path`: `https://example.com/api` connects with `path: '/api/socket.io'`. An `options.path` is appended to that prefix.
|
|
55
|
+
|
|
56
|
+
### Listen for entity events
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { EventNameSuffix, buildEventFullName } from '@authup/core-realtime-kit';
|
|
60
|
+
|
|
61
|
+
socket.emit(buildEventFullName('user', EventNameSuffix.SUBSCRIBE));
|
|
62
|
+
|
|
63
|
+
socket.on(buildEventFullName('user', EventNameSuffix.CREATED), (payload) => {
|
|
64
|
+
// payload.data is the created user, payload.meta.roomName the room it was sent to
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`buildEventFullName(entity, verb)` concatenates the entity name with the capitalized verb: `('user', 'created')` is `userCreated`.
|
|
69
|
+
|
|
70
|
+
### Reconnect after a token change
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
await manager.reconnectAll();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The token is only read on the handshake, so a socket keeps the token it connected with. `reconnect(namespace)` and `reconnectAll()` disconnect and connect again to send the current one. `disconnect(namespace)` closes a socket and keeps it for later; `eject(namespace)` closes and forgets it.
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
`new ClientManager(ctx)`:
|
|
81
|
+
|
|
82
|
+
| Option | Type | Default | Description |
|
|
83
|
+
|---|---|---|---|
|
|
84
|
+
| `url` | `string` | required | Server URL; its path becomes the socket.io path prefix |
|
|
85
|
+
| `token` | `string \| () => string \| undefined \| Promise<string \| undefined>` | | Sent as `auth.token` on every handshake |
|
|
86
|
+
| `options` | `Partial<ManagerOptions>` | | Passed to the socket.io-client `Manager` |
|
|
87
|
+
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
| Export | Kind | Purpose |
|
|
91
|
+
|---|---|---|
|
|
92
|
+
| `ClientManager` | class | One socket.io `Manager`, one socket per namespace: `connect`, `disconnect`, `reconnect`, `reconnectAll`, `inject`, `eject`, `options` |
|
|
93
|
+
| `ClientManagerContext`, `ClientManagerTokenFn` | type | Constructor input and the token function |
|
|
94
|
+
| `buildEventFullName` | function | `entity` + capitalized `verb` |
|
|
95
|
+
| `EventNameSuffix` | enum | `created`, `updated`, `deleted`, `subscribe`, `unsubscribe` |
|
|
96
|
+
| `EventFullName`, `EventPayload` | type | The event name template and the payload shape |
|
|
97
|
+
| `isEventTarget`, `isEventCallback` | function | Guards for socket handler arguments |
|
|
98
|
+
| `STCEvents`, `CTSEvents`, `STSEvents`, `STCEventContext`, `EventCallback`, `DefaultEvents` | type | Event map types for typed sockets |
|
|
17
99
|
|
|
18
100
|
## License
|
|
19
101
|
|
|
20
102
|
Made with ๐
|
|
21
103
|
|
|
22
|
-
Published under [Apache 2.0 License](./LICENSE).
|
|
104
|
+
Published under the [Apache 2.0 License](./LICENSE).
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@authup/core-realtime-kit",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.0-beta.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "1.0.0-beta.71",
|
|
5
|
+
"description": "A socket.io client manager and event-name helpers for Authup's realtime entity events.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"exports": {
|
|
8
8
|
"./package.json": "./package.json",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"homepage": "https://github.com/authup/authup#readme",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@authup/kit": "^1.0.0-beta.
|
|
54
|
+
"@authup/kit": "^1.0.0-beta.71"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"socket.io": "^4.8.4",
|