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