@fieldnotes/sync-redis 0.4.0 → 0.6.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 +159 -152
- package/dist/index.cjs +385 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -3
- package/dist/index.d.ts +39 -3
- package/dist/index.js +389 -1
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,152 +1,159 @@
|
|
|
1
|
-
# @fieldnotes/sync-redis
|
|
2
|
-
|
|
3
|
-
A `HubBackend` that persists Field Notes relay room state in Redis — state survives relay restarts and is
|
|
4
|
-
shared across relay instances.
|
|
5
|
-
|
|
6
|
-
## Install
|
|
7
|
-
|
|
8
|
-
```bash
|
|
9
|
-
pnpm add @fieldnotes/sync-redis
|
|
10
|
-
# plus your Redis client of choice, e.g.:
|
|
11
|
-
pnpm add redis # node-redis v4
|
|
12
|
-
# or
|
|
13
|
-
pnpm add ioredis
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
`@fieldnotes/sync-redis` has **no Redis dependency of its own** — you inject a client that satisfies a
|
|
17
|
-
minimal `RedisHashClient` interface (`hGetAll` / `hGet` / `hSet` / `hDel` / `del`).
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
import
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
1
|
+
# @fieldnotes/sync-redis
|
|
2
|
+
|
|
3
|
+
A `HubBackend` that persists Field Notes relay room state in Redis — state survives relay restarts and is
|
|
4
|
+
shared across relay instances.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @fieldnotes/sync-redis
|
|
10
|
+
# plus your Redis client of choice, e.g.:
|
|
11
|
+
pnpm add redis # node-redis v4
|
|
12
|
+
# or
|
|
13
|
+
pnpm add ioredis
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`@fieldnotes/sync-redis` has **no Redis dependency of its own** — you inject a client that satisfies a
|
|
17
|
+
minimal `RedisHashClient` interface (`hGetAll` / `hGet` / `hSet` / `hDel` / `del` / `eval`).
|
|
18
|
+
`eval` is required for atomic fog-of-war updates.
|
|
19
|
+
|
|
20
|
+
## node-redis v4 (direct)
|
|
21
|
+
|
|
22
|
+
node-redis v4's client already matches `RedisHashClient`, so you can pass it straight through:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { createClient } from 'redis';
|
|
26
|
+
import { createSyncServer } from '@fieldnotes/sync-server';
|
|
27
|
+
import { RedisHubBackend } from '@fieldnotes/sync-redis';
|
|
28
|
+
|
|
29
|
+
const client = createClient({ url: process.env.REDIS_URL });
|
|
30
|
+
await client.connect();
|
|
31
|
+
createSyncServer({ port: 8080, backend: new RedisHubBackend(client) });
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## ioredis (shim)
|
|
35
|
+
|
|
36
|
+
ioredis uses lowercased method names, so wrap it in a small adapter:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import Redis from 'ioredis';
|
|
40
|
+
import { RedisHubBackend } from '@fieldnotes/sync-redis';
|
|
41
|
+
|
|
42
|
+
const io = new Redis(process.env.REDIS_URL);
|
|
43
|
+
const client = {
|
|
44
|
+
hGetAll: (k: string) => io.hgetall(k),
|
|
45
|
+
hGet: (k: string, f: string) => io.hget(k, f),
|
|
46
|
+
hSet: (k: string, f: string, v: string) => io.hset(k, f, v),
|
|
47
|
+
hDel: (k: string, f: string) => io.hdel(k, f),
|
|
48
|
+
del: (k: string) => io.del(k),
|
|
49
|
+
eval: (script: string, options: { keys: string[]; arguments: string[] }) =>
|
|
50
|
+
io.eval(script, options.keys.length, ...options.keys, ...options.arguments),
|
|
51
|
+
};
|
|
52
|
+
const backend = new RedisHubBackend(client);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Key schema
|
|
56
|
+
|
|
57
|
+
Each room is stored as a Redis **HASH** at `{keyPrefix}{room}` (default prefix `fieldnotes:room:`):
|
|
58
|
+
|
|
59
|
+
- **field** = element id
|
|
60
|
+
- **value** = `JSON.stringify(element)`
|
|
61
|
+
|
|
62
|
+
Fog uses two additional hashes, `${key}:fog:meta` and `${key}:fog:tiles`, and atomic Lua scripts via
|
|
63
|
+
`EVAL`; custom Redis adapters must expose the node-redis-compatible `eval(script, { keys, arguments })`
|
|
64
|
+
shape shown above.
|
|
65
|
+
|
|
66
|
+
node-redis v4 conforms to `RedisHashClient` directly (it has `hGet`); `RedisHubBackend.get(room, id)`
|
|
67
|
+
(via `HGET`) powers the relay's ownership lookups for write authorization (D2).
|
|
68
|
+
|
|
69
|
+
The prefix is configurable:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
new RedisHubBackend(client, { keyPrefix: 'myapp:room:' });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Single vs. multiple relay instances
|
|
76
|
+
|
|
77
|
+
Persistence and shared state work with any number of relay instances — every instance reads and writes the
|
|
78
|
+
same Redis, so room state survives restarts and is visible to all of them.
|
|
79
|
+
|
|
80
|
+
For clients connected to **different** relay instances to see each other's **live** ops, you also need
|
|
81
|
+
cross-instance fan-out (Redis pub/sub) — available via `RedisHubFanout` (below). A **single** relay instance
|
|
82
|
+
is fully live on its own.
|
|
83
|
+
|
|
84
|
+
## Cross-instance fan-out (`RedisHubFanout`)
|
|
85
|
+
|
|
86
|
+
`RedisHubFanout` is a `HubFanout` (from `@fieldnotes/sync-server`) over Redis pub/sub: each relay instance
|
|
87
|
+
publishes its live ops to a single channel and every other instance forwards them to its local connections.
|
|
88
|
+
Like the backend, it has **no Redis dependency of its own** — you inject two connections via the
|
|
89
|
+
`RedisPublisher` / `RedisSubscriber` seams.
|
|
90
|
+
|
|
91
|
+
`publish()` resolves only after Redis acknowledges the command. If Redis rejects it, the fanout
|
|
92
|
+
invokes the optional `onError` observer and rethrows so `SyncHub` can surface the failed durable
|
|
93
|
+
mutation publication. Do not discard the returned promise when publishing directly.
|
|
94
|
+
|
|
95
|
+
> **The subscriber MUST be a dedicated connection.** Redis forbids any other command on a connection that is
|
|
96
|
+
> in subscribe mode, so publish and subscribe cannot share one connection — that is why `RedisHubFanout`
|
|
97
|
+
> takes two.
|
|
98
|
+
|
|
99
|
+
### node-redis v4
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { createClient } from 'redis';
|
|
103
|
+
import { createSyncServer } from '@fieldnotes/sync-server';
|
|
104
|
+
import { RedisHubBackend, RedisHubFanout } from '@fieldnotes/sync-redis';
|
|
105
|
+
|
|
106
|
+
const hash = createClient({ url: process.env.REDIS_URL });
|
|
107
|
+
await hash.connect(); // HASH backend (persistence)
|
|
108
|
+
|
|
109
|
+
const publisher = createClient({ url: process.env.REDIS_URL });
|
|
110
|
+
await publisher.connect();
|
|
111
|
+
const subscriber = publisher.duplicate();
|
|
112
|
+
await subscriber.connect(); // DEDICATED subscriber — a subscriber connection cannot also publish
|
|
113
|
+
|
|
114
|
+
createSyncServer({
|
|
115
|
+
port: 8080,
|
|
116
|
+
backend: new RedisHubBackend(hash),
|
|
117
|
+
fanout: new RedisHubFanout(publisher, subscriber),
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### ioredis
|
|
122
|
+
|
|
123
|
+
ioredis lowercases `publish`/`subscribe` and delivers messages via a `'message'` event, so wrap each client:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import Redis from 'ioredis';
|
|
127
|
+
import { RedisHubFanout } from '@fieldnotes/sync-redis';
|
|
128
|
+
|
|
129
|
+
const pubIo = new Redis(process.env.REDIS_URL); // publish connection
|
|
130
|
+
const subIo = new Redis(process.env.REDIS_URL); // SECOND, dedicated subscriber connection
|
|
131
|
+
|
|
132
|
+
const publisher = { publish: (c: string, m: string) => pubIo.publish(c, m) };
|
|
133
|
+
const subscriber = {
|
|
134
|
+
subscribe: (channel: string, listener: (m: string) => void) => {
|
|
135
|
+
subIo.on('message', (ch, msg) => {
|
|
136
|
+
if (ch === channel) listener(msg);
|
|
137
|
+
});
|
|
138
|
+
return subIo.subscribe(channel);
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const fanout = new RedisHubFanout(publisher, subscriber);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Channel
|
|
146
|
+
|
|
147
|
+
All instances must share one channel (default `fieldnotes:fanout`, configurable):
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
new RedisHubFanout(publisher, subscriber, { channel: 'myapp:fanout' });
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Precondition: shared fanout AND shared backend
|
|
154
|
+
|
|
155
|
+
Multi-instance **live** sync requires **both** a shared fanout **and** a shared backend (both Redis). The
|
|
156
|
+
fanout forwards live ops; the shared backend keeps snapshots consistent. A shared fanout **without** a shared
|
|
157
|
+
backend leaves a new joiner's snapshot stale — it would catch up from whichever instance it happened to hit,
|
|
158
|
+
missing edits applied elsewhere. Pair `RedisHubFanout` with `RedisHubBackend` for full multi-instance
|
|
159
|
+
real-time sync.
|