@devvit/realtime 0.12.1-next-2025-09-17-19-53-43-a7752f728.0 → 0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.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.
@@ -1,7 +1,18 @@
1
1
  import type { JsonValue } from '@devvit/shared';
2
2
  export declare class RealtimeClient {
3
3
  #private;
4
- send(channel: string, msg: JsonValue): Promise<void>;
4
+ /**
5
+ * @example
6
+ *
7
+ * ```ts
8
+ * import {context, realtime} from '@devvit/web/server';
9
+ * import type {T2} from '@devvit/web/shared'
10
+ *
11
+ * type RealtimeMessage = {x: number, y: number, t2: T2}
12
+ *
13
+ * await realtime.send<RealtimeMessage>({x: 1, y: 2, t2: context.userId})
14
+ * ```
15
+ */
16
+ send<Msg extends JsonValue>(channel: string, msg: Msg): Promise<void>;
5
17
  }
6
- export declare const realtime: RealtimeClient;
7
18
  //# sourceMappingURL=RealtimeClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"RealtimeClient.d.ts","sourceRoot":"","sources":["../src/RealtimeClient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,qBAAa,cAAc;;IAGnB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ3D;AAED,eAAO,MAAM,QAAQ,gBAAuB,CAAC"}
1
+ {"version":3,"file":"RealtimeClient.d.ts","sourceRoot":"","sources":["../src/RealtimeClient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,qBAAa,cAAc;;IAGzB;;;;;;;;;;;OAWG;IACG,IAAI,CAAC,GAAG,SAAS,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ5E"}
package/RealtimeClient.js CHANGED
@@ -18,6 +18,18 @@ export class RealtimeClient {
18
18
  _RealtimeClient_instances.add(this);
19
19
  _RealtimeClient_pluginCache.set(this, void 0);
20
20
  }
21
+ /**
22
+ * @example
23
+ *
24
+ * ```ts
25
+ * import {context, realtime} from '@devvit/web/server';
26
+ * import type {T2} from '@devvit/web/shared'
27
+ *
28
+ * type RealtimeMessage = {x: number, y: number, t2: T2}
29
+ *
30
+ * await realtime.send<RealtimeMessage>({x: 1, y: 2, t2: context.userId})
31
+ * ```
32
+ */
21
33
  async send(channel, msg) {
22
34
  // guarantee an object by wrapping msg. the key must align to useChannel().
23
35
  await __classPrivateFieldGet(this, _RealtimeClient_instances, "a", _RealtimeClient_plugin_get).Send({ channel, data: { msg } }, context.metadata);
@@ -26,4 +38,3 @@ export class RealtimeClient {
26
38
  _RealtimeClient_pluginCache = new WeakMap(), _RealtimeClient_instances = new WeakSet(), _RealtimeClient_plugin_get = function _RealtimeClient_plugin_get() {
27
39
  return (__classPrivateFieldSet(this, _RealtimeClient_pluginCache, __classPrivateFieldGet(this, _RealtimeClient_pluginCache, "f") ?? getDevvitConfig().use(RealtimeDefinition), "f"));
28
40
  };
29
- export const realtime = new RealtimeClient();
package/client.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { connectRealtime } from './realtime.js';
1
+ export { type Connection, connectRealtime, type ConnectRealtimeOptions, disconnectRealtime, } from './realtime.js';
2
2
  //# sourceMappingURL=client.d.ts.map
package/client.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,eAAe,EACf,KAAK,sBAAsB,EAC3B,kBAAkB,GACnB,MAAM,eAAe,CAAC"}
package/client.js CHANGED
@@ -1 +1 @@
1
- export { connectRealtime } from './realtime.js';
1
+ export { connectRealtime, disconnectRealtime, } from './realtime.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devvit/realtime",
3
- "version": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
3
+ "version": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
4
4
  "license": "BSD-3-Clause",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,22 +35,22 @@
35
35
  "test:unit-with-coverage": "vitest run --coverage"
36
36
  },
37
37
  "dependencies": {
38
- "@devvit/protos": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
39
- "@devvit/shared": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
40
- "@devvit/shared-types": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0"
38
+ "@devvit/protos": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
39
+ "@devvit/shared": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
40
+ "@devvit/shared-types": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@devvit/client": "*",
44
44
  "@devvit/server": "*"
45
45
  },
46
46
  "devDependencies": {
47
- "@devvit/client": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
48
- "@devvit/repo-tools": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
49
- "@devvit/server": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
50
- "@devvit/tsconfig": "0.12.1-next-2025-09-17-19-53-43-a7752f728.0",
47
+ "@devvit/client": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
48
+ "@devvit/repo-tools": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
49
+ "@devvit/server": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
50
+ "@devvit/tsconfig": "0.12.1-next-2025-09-22-19-00-17-eb5ffb2a8.0",
51
51
  "eslint": "9.11.1",
52
52
  "typescript": "5.8.3",
53
53
  "vitest": "1.6.1"
54
54
  },
55
- "gitHead": "94bf4342af792bc0ea4433cac03c90ac14c2998f"
55
+ "gitHead": "a62a0afda055b6c8663203e970acddcf15f2027a"
56
56
  }
package/realtime.d.ts CHANGED
@@ -1,36 +1,58 @@
1
1
  import type { WebViewMessageEvent_MessageData } from '@devvit/protos/json/devvit/ui/events/v1alpha/web_view.js';
2
2
  import type { JsonValue } from '@devvit/shared';
3
- type ConnectRealtimeOptions = {
3
+ export type ConnectRealtimeOptions<Msg extends JsonValue> = {
4
4
  channel: string;
5
5
  onConnect?: (channel: string) => void;
6
6
  onDisconnect?: (channel: string) => void;
7
- onMessage: (data: JsonValue) => void;
7
+ onMessage: (data: Msg) => void;
8
8
  };
9
9
  /**
10
10
  * Connects to a realtime channel for receiving messages.
11
11
  *
12
12
  * @param opts - Connection options including channel name and callbacks
13
13
  * @returns A Connection object with a disconnect method
14
+ *
15
+ * @example
16
+ *
17
+ * ```ts
18
+ * import {connectRealtime, context} from '@devvit/web/client'
19
+ * import type {T2} from '@devvit/web/shared'
20
+ *
21
+ * type RealtimeMessage = {x: number, y: number, t2: T2}
22
+ *
23
+ * connectRealtime<RealtimeMessage>({
24
+ * channel: context.postId,
25
+ * onConnect() {
26
+ * console.log(`${context.userId} connected`)
27
+ * },
28
+ * onDisconnect() {
29
+ * console.log(`${context.userId} disconnected`)
30
+ * },
31
+ * onMessage(msg) {
32
+ * // Filter out current user across all sessions.
33
+ * if (msg.t2 === context.userId) return
34
+ *
35
+ * console.log(`user ${msg.t2} at (${msg.x}, ${msg.y})`)
36
+ * }
37
+ * })
38
+ * ```
14
39
  */
15
- export declare const connectRealtime: (opts: Readonly<ConnectRealtimeOptions>) => Promise<Connection>;
40
+ export declare const connectRealtime: <Msg extends JsonValue>(opts: Readonly<ConnectRealtimeOptions<Msg>>) => Promise<Connection>;
41
+ export declare function disconnectRealtime(channel: string): Promise<void>;
16
42
  /**
17
43
  * Clears all connections. Used for testing purposes.
18
44
  * @internal
19
45
  */
20
46
  export declare const __clearConnections: () => void;
21
- declare class Connection {
47
+ export declare class Connection {
22
48
  #private;
23
- constructor(opts: Readonly<ConnectRealtimeOptions>);
49
+ constructor(opts: Readonly<ConnectRealtimeOptions<JsonValue>>);
24
50
  /**
25
51
  * Disconnects from the realtime channel.
26
52
  *
27
- * This works by sending a list of all channels we want to remain subscribed to,
28
- * excluding the channel we want to disconnect from. The effect handler compares this
29
- * new list with existing subscriptions and triggers disconnect for any channel
30
- * that's no longer in the list.
53
+ * @deprecated Use `disconnectRealtime()`.
31
54
  */
32
55
  disconnect(): Promise<void>;
33
56
  onMessage: (ev: MessageEvent<WebViewMessageEvent_MessageData>) => void;
34
57
  }
35
- export {};
36
58
  //# sourceMappingURL=realtime.d.ts.map
package/realtime.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"realtime.d.ts","sourceRoot":"","sources":["../src/realtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,0DAA0D,CAAC;AAGhH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,KAAK,sBAAsB,GAAG;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,SAAS,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,IAAI,CAAC;CACtC,CAAC;AAQF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAC1B,MAAM,QAAQ,CAAC,sBAAsB,CAAC,KACrC,OAAO,CAAC,UAAU,CAepB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,QAAO,IAErC,CAAC;AAEF,cAAM,UAAU;;gBAGF,IAAI,EAAE,QAAQ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@ const connectionsByChannel = new Map();
20
20
  *
21
21
  * @param opts - Connection options including channel name and callbacks
22
22
  * @returns A Connection object with a disconnect method
23
+ *
24
+ * @example
25
+ *
26
+ * ```ts
27
+ * import {connectRealtime, context} from '@devvit/web/client'
28
+ * import type {T2} from '@devvit/web/shared'
29
+ *
30
+ * type RealtimeMessage = {x: number, y: number, t2: T2}
31
+ *
32
+ * connectRealtime<RealtimeMessage>({
33
+ * channel: context.postId,
34
+ * onConnect() {
35
+ * console.log(`${context.userId} connected`)
36
+ * },
37
+ * onDisconnect() {
38
+ * console.log(`${context.userId} disconnected`)
39
+ * },
40
+ * onMessage(msg) {
41
+ * // Filter out current user across all sessions.
42
+ * if (msg.t2 === context.userId) return
43
+ *
44
+ * console.log(`user ${msg.t2} at (${msg.x}, ${msg.y})`)
45
+ * }
46
+ * })
47
+ * ```
23
48
  */
24
49
  export const connectRealtime = async (opts) => {
25
50
  if (connectionsByChannel.has(opts.channel)) {
@@ -28,12 +53,17 @@ export const connectRealtime = async (opts) => {
28
53
  const connection = new Connection(opts);
29
54
  connectionsByChannel.set(opts.channel, connection);
30
55
  addEventListener('message', connection.onMessage);
31
- await emitEffect({
32
- realtimeSubscriptions: { subscriptionIds: [...connectionsByChannel.keys()] },
33
- type: 0,
34
- });
56
+ await emitConnectionsEffect();
35
57
  return connection;
36
58
  };
59
+ export async function disconnectRealtime(channel) {
60
+ const connection = connectionsByChannel.get(channel);
61
+ if (!connection)
62
+ return;
63
+ connectionsByChannel.delete(channel);
64
+ removeEventListener('message', connection.onMessage);
65
+ await emitConnectionsEffect();
66
+ }
37
67
  /**
38
68
  * Clears all connections. Used for testing purposes.
39
69
  * @internal
@@ -41,7 +71,14 @@ export const connectRealtime = async (opts) => {
41
71
  export const __clearConnections = () => {
42
72
  connectionsByChannel.clear();
43
73
  };
44
- class Connection {
74
+ /** Emits open connections which may cause subscribe / unsubscribe. */
75
+ async function emitConnectionsEffect() {
76
+ await emitEffect({
77
+ realtimeSubscriptions: { subscriptionIds: [...connectionsByChannel.keys()] },
78
+ type: 0,
79
+ });
80
+ }
81
+ export class Connection {
45
82
  constructor(opts) {
46
83
  _Connection_opts.set(this, void 0);
47
84
  this.onMessage = (ev) => {
@@ -79,23 +116,10 @@ class Connection {
79
116
  /**
80
117
  * Disconnects from the realtime channel.
81
118
  *
82
- * This works by sending a list of all channels we want to remain subscribed to,
83
- * excluding the channel we want to disconnect from. The effect handler compares this
84
- * new list with existing subscriptions and triggers disconnect for any channel
85
- * that's no longer in the list.
119
+ * @deprecated Use `disconnectRealtime()`.
86
120
  */
87
121
  async disconnect() {
88
- const connection = connectionsByChannel.get(__classPrivateFieldGet(this, _Connection_opts, "f").channel);
89
- if (!connection) {
90
- return;
91
- }
92
- // Get all current subscriptions except the one we're disconnecting
93
- const remainingChannels = [...connectionsByChannel.keys()].filter((ch) => ch !== __classPrivateFieldGet(this, _Connection_opts, "f").channel);
94
- await emitEffect({
95
- realtimeSubscriptions: { subscriptionIds: remainingChannels },
96
- type: 0,
97
- });
98
- connectionsByChannel.delete(__classPrivateFieldGet(this, _Connection_opts, "f").channel);
122
+ await disconnectRealtime(__classPrivateFieldGet(this, _Connection_opts, "f").channel);
99
123
  }
100
124
  }
101
125
  _Connection_opts = new WeakMap();
package/server.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- export { realtime, type RealtimeClient } from './RealtimeClient.js';
1
+ import { RealtimeClient } from './RealtimeClient.js';
2
+ export type { RealtimeClient } from './RealtimeClient.js';
3
+ export declare const realtime: RealtimeClient;
2
4
  //# sourceMappingURL=server.d.ts.map
package/server.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,eAAO,MAAM,QAAQ,EAAE,cAAqC,CAAC"}
package/server.js CHANGED
@@ -1 +1,2 @@
1
- export { realtime } from './RealtimeClient.js';
1
+ import { RealtimeClient } from './RealtimeClient.js';
2
+ export const realtime = new RealtimeClient();