@base44-preview/sdk 0.8.36-pr.212.0d64c77 → 0.8.36-pr.219.4b81d03

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/dist/client.js CHANGED
@@ -12,7 +12,6 @@ import { createAppLogsModule } from "./modules/app-logs.js";
12
12
  import { createUsersModule } from "./modules/users.js";
13
13
  import { RoomsSocket } from "./utils/socket-utils.js";
14
14
  import { createAnalyticsModule } from "./modules/analytics.js";
15
- import { createRealtimeModule, pushUserTokenToActiveSockets } from "./modules/realtime.js";
16
15
  /**
17
16
  * Creates a Base44 client.
18
17
  *
@@ -52,19 +51,9 @@ import { createRealtimeModule, pushUserTokenToActiveSockets } from "./modules/re
52
51
  */
53
52
  export function createClient(config) {
54
53
  var _a, _b;
55
- const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, dispatcherWsUrl, webSocketImpl, } = config;
54
+ const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
56
55
  // Normalize appBaseUrl to always be a string (empty if not provided or invalid)
57
56
  const normalizedAppBaseUrl = typeof appBaseUrl === "string" ? appBaseUrl : "";
58
- // Derive the dispatcher WebSocket URL from serverUrl if not explicitly provided.
59
- // Convert https:// → wss:// (or http:// → ws://) and strip trailing slash.
60
- const resolvedDispatcherWsUrl = (() => {
61
- if (dispatcherWsUrl)
62
- return dispatcherWsUrl.replace(/\/$/, "");
63
- return serverUrl
64
- .replace(/\/$/, "")
65
- .replace(/^https:\/\//, "wss://")
66
- .replace(/^http:\/\//, "ws://");
67
- })();
68
57
  const socketConfig = {
69
58
  serverUrl,
70
59
  mountPath: "/ws-user-apps/socket.io/",
@@ -124,21 +113,6 @@ export function createClient(config) {
124
113
  appBaseUrl: normalizedAppBaseUrl,
125
114
  serverUrl,
126
115
  });
127
- // Current user session token (axios defaults are the single source of truth —
128
- // createClient({token}) and every setToken() land there). Used for in-band
129
- // realtime auth; read lazily so refreshes are always picked up.
130
- const getUserToken = () => {
131
- var _a;
132
- const h = (_a = axiosClient.defaults.headers.common) === null || _a === void 0 ? void 0 : _a["Authorization"];
133
- return typeof h === "string" && h.startsWith("Bearer ") ? h.slice(7) : null;
134
- };
135
- // Login / token refresh must reach long-lived realtime sockets too, so the
136
- // handler-side credential never goes stale mid-connection.
137
- const originalSetToken = userAuthModule.setToken.bind(userAuthModule);
138
- userAuthModule.setToken = (newToken, saveToStorage) => {
139
- originalSetToken(newToken, saveToStorage);
140
- pushUserTokenToActiveSockets(newToken);
141
- };
142
116
  // Apply the access token before any module that may issue authenticated
143
117
  // requests during construction (notably analytics, which fires an init
144
118
  // event whose flush calls auth.me()). Without this, the first User/me
@@ -186,31 +160,6 @@ export function createClient(config) {
186
160
  appId,
187
161
  userAuthModule,
188
162
  }),
189
- realtime: createRealtimeModule({
190
- appId,
191
- dispatcherWsUrl: resolvedDispatcherWsUrl,
192
- webSocketImpl,
193
- getUserToken,
194
- getToken: async (handlerName, instanceId, connId) => {
195
- // axiosClient interceptors unwrap response.data, so the result is the body directly.
196
- // conn_id rides inside the signed token (not a WS query param) so it survives
197
- // proxies that strip params; the dispatcher forwards it as the handler's conn.id.
198
- // Base44-Functions-Version rides along (like function calls) so live apps get
199
- // tokens for the *published* realtime script and previews get the draft.
200
- const data = await axiosClient.post(`/apps/${appId}/realtime-token`, {
201
- handler_name: handlerName,
202
- instance_id: instanceId,
203
- conn_id: connId,
204
- // Declares "an __auth message follows right after connect" — the
205
- // handler delays handleConnect until it arrives (signed into the
206
- // token so old SDKs, which never send __auth, are never waited on).
207
- supports_inband_auth: getUserToken() != null,
208
- }, functionsVersion
209
- ? { headers: { "Base44-Functions-Version": functionsVersion } }
210
- : undefined);
211
- return data.token;
212
- },
213
- }),
214
163
  cleanup: () => {
215
164
  userModules.analytics.cleanup();
216
165
  if (socket) {
@@ -8,7 +8,6 @@ import type { AgentsModule } from "./modules/agents.types.js";
8
8
  import type { AiGatewayModule } from "./modules/ai-gateway.types.js";
9
9
  import type { AppLogsModule } from "./modules/app-logs.types.js";
10
10
  import type { AnalyticsModule } from "./modules/analytics.types.js";
11
- import type { RealtimeModule } from "./modules/realtime.types.js";
12
11
  /**
13
12
  * Options for creating a Base44 client.
14
13
  */
@@ -74,25 +73,6 @@ export interface CreateClientConfig {
74
73
  * Additional client options.
75
74
  */
76
75
  options?: CreateClientOptions;
77
- /**
78
- * Base WebSocket URL for the Cloudflare Durable Object dispatcher.
79
- *
80
- * Defaults to the `serverUrl` with `https://` replaced by `wss://` (or `http://` by `ws://`).
81
- * Override when the dispatcher lives at a different host than the API.
82
- */
83
- dispatcherWsUrl?: string;
84
- /**
85
- * WebSocket implementation for realtime subscriptions in environments
86
- * without a global `WebSocket` (Node.js < 22). Browsers and Node ≥ 22
87
- * don't need this.
88
- *
89
- * @example
90
- * ```typescript
91
- * import WS from "ws";
92
- * const base44 = createClient({ appId, webSocketImpl: WS });
93
- * ```
94
- */
95
- webSocketImpl?: unknown;
96
76
  }
97
77
  /**
98
78
  * The Base44 client instance.
@@ -108,8 +88,6 @@ export interface Base44Client {
108
88
  analytics: AnalyticsModule;
109
89
  /** {@link AppLogsModule | App logs module} for tracking app usage. */
110
90
  appLogs: AppLogsModule;
111
- /** {@link RealtimeModule | Realtime module} for subscribing to and sending messages via Cloudflare Durable Object-backed RealtimeHandlers. */
112
- realtime: RealtimeModule;
113
91
  /** {@link AuthModule | Auth module} for user authentication and management. */
114
92
  auth: AuthModule;
115
93
  /** {@link UserConnectorsModule | Connectors module} for app-user OAuth flows. */
package/dist/index.d.ts CHANGED
@@ -11,9 +11,7 @@ export type { FunctionsModule, FunctionName, FunctionNameRegistry, } from "./mod
11
11
  export type { AgentsModule, AgentName, AgentNameRegistry, AgentConversation, AgentMessage, AgentMessageReasoning, AgentMessageToolCall, AgentMessageUsage, AgentMessageCustomContext, AgentMessageMetadata, CreateConversationParams, } from "./modules/agents.types.js";
12
12
  export type { AiGatewayModule, AiGatewayConnection, } from "./modules/ai-gateway.types.js";
13
13
  export type { AppLogsModule } from "./modules/app-logs.types.js";
14
- export type { RealtimeModule, RealtimeHandlerClient, RealtimeHandlerNameRegistry, RealtimeHandlerRegistry, } from "./modules/realtime.types.js";
15
14
  export type { SsoModule, SsoAccessTokenResponse } from "./modules/sso.types.js";
16
- export { RealtimeHandler, type Conn } from "./realtime-handler.js";
17
15
  export type { ConnectorsModule, UserConnectorsModule, } from "./modules/connectors.types.js";
18
16
  export type { CustomIntegrationsModule, CustomIntegrationCallParams, CustomIntegrationCallResponse, } from "./modules/custom-integrations.types.js";
19
17
  export type { GetAccessTokenOptions, SaveAccessTokenOptions, RemoveAccessTokenOptions, GetLoginUrlOptions, } from "./utils/auth-utils.types.js";
package/dist/index.js CHANGED
@@ -3,4 +3,3 @@ import { Base44Error } from "./utils/axios-client.js";
3
3
  import { getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, } from "./utils/auth-utils.js";
4
4
  export { createClient, createClientFromRequest, Base44Error, getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, };
5
5
  export * from "./types.js";
6
- export { RealtimeHandler } from "./realtime-handler.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.36-pr.212.0d64c77",
3
+ "version": "0.8.36-pr.219.4b81d03",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,7 +27,6 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "axios": "^1.17.0",
30
- "partysocket": "^0.0.23",
31
30
  "socket.io-client": "^4.8.3",
32
31
  "uuid": "^13.0.2"
33
32
  },
@@ -1,28 +0,0 @@
1
- /** Push a (new) user session token to every open realtime socket — called on
2
- * login/refresh so long-lived connections keep a valid credential server-side. */
3
- export declare function pushUserTokenToActiveSockets(token: string): void;
4
- export declare function createRealtimeModule(config: {
5
- appId: string;
6
- getToken(handlerName: string, instanceId: string, connId: string): Promise<string>;
7
- /** Current user session token, if signed in. Sent in-band ({type:"__auth"})
8
- * right after every socket open — never in the URL — so the handler can act
9
- * as this user (createUserClient / RLS). */
10
- getUserToken?: () => string | null;
11
- dispatcherWsUrl: string;
12
- /** WebSocket implementation for runtimes without a global one (Node < 22). */
13
- webSocketImpl?: unknown;
14
- }): Record<string, RealtimeHandler>;
15
- /** Handle for an active realtime subscription. */
16
- interface RealtimeSubscription {
17
- /** This connection's id — the same value the handler receives as `conn.id`. */
18
- id: string;
19
- /** Close the subscription and its underlying socket. */
20
- unsubscribe(): void;
21
- }
22
- interface RealtimeHandler {
23
- subscribe(instanceId: string, callback: (data: unknown) => void, options?: {
24
- id?: string;
25
- }): RealtimeSubscription;
26
- send(instanceId: string, data: unknown): void;
27
- }
28
- export {};
@@ -1,124 +0,0 @@
1
- import PartySocket from "partysocket";
2
- // Module-level map: "HandlerName:instanceId" → active socket
3
- const activeSockets = new Map();
4
- function socketKey(handlerName, instanceId) {
5
- return `${handlerName}:${instanceId}`;
6
- }
7
- /** Push a (new) user session token to every open realtime socket — called on
8
- * login/refresh so long-lived connections keep a valid credential server-side. */
9
- export function pushUserTokenToActiveSockets(token) {
10
- if (!token)
11
- return;
12
- const payload = JSON.stringify({ type: "__auth", token });
13
- for (const ws of activeSockets.values()) {
14
- try {
15
- ws.send(payload);
16
- }
17
- catch ( /* not open — the open handler will send */_a) { /* not open — the open handler will send */ }
18
- }
19
- }
20
- export function createRealtimeModule(config) {
21
- return new Proxy({}, {
22
- get(_, handlerName) {
23
- return {
24
- subscribe(instanceId, callback, options) {
25
- var _a, _b;
26
- const key = socketKey(handlerName, instanceId);
27
- // close existing if any
28
- (_a = activeSockets.get(key)) === null || _a === void 0 ? void 0 : _a.close();
29
- // Connection id: caller-supplied (stable — reuse across reconnects/tabs as
30
- // you see fit) or auto-generated per subscription. It travels INSIDE the
31
- // signed realtime token (never as a WS query param, which proxies strip);
32
- // the dispatcher forwards the verified claim as partyserver's _pk, so the
33
- // handler sees this exact value as conn.id. Reconnects re-mint the token
34
- // with the same id, so conn.id is stable across reconnects.
35
- const connId = (_b = options === null || options === void 0 ? void 0 : options.id) !== null && _b !== void 0 ? _b : crypto.randomUUID();
36
- // query as async fn: called on every (re)connect, fetches a fresh token each time
37
- const ws = new PartySocket({
38
- host: config.dispatcherWsUrl,
39
- party: handlerName,
40
- room: instanceId,
41
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
- ...(config.webSocketImpl ? { WebSocket: config.webSocketImpl } : {}),
43
- query: () => config.getToken(handlerName, instanceId, connId).then((token) => ({ token })),
44
- });
45
- activeSockets.set(key, ws);
46
- // In-band credential delivery (Supabase-style): the user token rides the
47
- // open socket, never the URL. Sent on every open (incl. reconnects); the
48
- // server may also nudge with {type:"__auth_required"} (e.g. just before
49
- // the held token expires) and we answer with the current one.
50
- const sendAuth = () => {
51
- var _a;
52
- const t = (_a = config.getUserToken) === null || _a === void 0 ? void 0 : _a.call(config);
53
- if (t) {
54
- try {
55
- ws.send(JSON.stringify({ type: "__auth", token: t }));
56
- }
57
- catch ( /* not open */_b) { /* not open */ }
58
- }
59
- };
60
- ws.addEventListener("open", sendAuth);
61
- // Heartbeat / half-open detection. PartySocket only reconnects on a
62
- // browser close/error event, so a silently-dead connection (TCP alive,
63
- // no data — common behind proxies/LBs) hangs until the OS idle timeout
64
- // (~60s). We ping periodically and force a reconnect if nothing comes
65
- // back within DEAD_MS, cutting detection from ~60s to a few seconds.
66
- // Pairs with the handler's setWebSocketAutoResponse("__ping"→"__pong"),
67
- // so idle handlers (no app broadcasts) still keep the connection proven.
68
- const PING_MS = 1000;
69
- const DEAD_MS = 3000;
70
- let lastMsg = Date.now();
71
- const bumpAlive = () => { lastMsg = Date.now(); };
72
- ws.addEventListener("open", bumpAlive);
73
- ws.addEventListener("message", (ev) => {
74
- bumpAlive();
75
- let data;
76
- try {
77
- data = JSON.parse(ev.data);
78
- }
79
- catch (_a) {
80
- return; // ignore malformed
81
- }
82
- // Swallow platform messages — never surface them to the app.
83
- const msgType = data && typeof data === "object" ? data.type : undefined;
84
- if (msgType === "__pong")
85
- return;
86
- if (msgType === "__auth_required") {
87
- sendAuth();
88
- return;
89
- }
90
- callback(data);
91
- });
92
- const heartbeat = setInterval(() => {
93
- if (Date.now() - lastMsg > DEAD_MS) {
94
- bumpAlive(); // avoid a reconnect storm while the new socket comes up
95
- ws.reconnect();
96
- return;
97
- }
98
- try {
99
- ws.send(JSON.stringify({ type: "__ping" }));
100
- }
101
- catch (_a) {
102
- // socket not open; the watchdog above will force a reconnect
103
- }
104
- }, PING_MS);
105
- return {
106
- id: connId, // the connection id (same value the handler sees as conn.id)
107
- unsubscribe() {
108
- clearInterval(heartbeat);
109
- activeSockets.delete(key);
110
- ws.close();
111
- },
112
- };
113
- },
114
- send(instanceId, data) {
115
- const key = socketKey(handlerName, instanceId);
116
- const ws = activeSockets.get(key);
117
- if (!ws)
118
- throw new Error(`No active subscription for ${handlerName}:${instanceId}`);
119
- ws.send(JSON.stringify(data));
120
- },
121
- };
122
- },
123
- });
124
- }
@@ -1,77 +0,0 @@
1
- /**
2
- * Extend this interface to add typed `subscribe` callbacks and `send` payloads
3
- * for your deployed RealtimeHandlers.
4
- *
5
- * This is separate from {@link RealtimeHandlerNameRegistry} (which is auto-generated
6
- * by `base44 types generate`), so there are no conflicts.
7
- *
8
- * @example
9
- * ```typescript
10
- * declare module "@base44/sdk" {
11
- * interface RealtimeHandlerRegistry {
12
- * ChatRoom: {
13
- * toClient: { type: "joined" | "left" | "message"; userId?: string; from?: string; text?: string };
14
- * toServer: { type: "message"; text: string };
15
- * };
16
- * }
17
- * }
18
- * ```
19
- */
20
- export interface RealtimeHandlerRegistry {
21
- }
22
- /**
23
- * Auto-populated by `base44 types generate` with the names of your deployed handlers.
24
- * Do not edit this interface manually — use {@link RealtimeHandlerRegistry} for message types.
25
- */
26
- export interface RealtimeHandlerNameRegistry {
27
- }
28
- type AllHandlerNames = keyof RealtimeHandlerRegistry | keyof RealtimeHandlerNameRegistry;
29
- type ToClientFor<N extends string> = N extends keyof RealtimeHandlerRegistry ? RealtimeHandlerRegistry[N] extends {
30
- toClient: infer I;
31
- } ? I : unknown : unknown;
32
- type ToServerFor<N extends string> = N extends keyof RealtimeHandlerRegistry ? RealtimeHandlerRegistry[N] extends {
33
- toServer: infer O;
34
- } ? O : unknown : unknown;
35
- /**
36
- * Client for a single named RealtimeHandler.
37
- * Typed automatically when the handler is registered in {@link RealtimeHandlerRegistry}.
38
- */
39
- export interface RealtimeHandlerClient<N extends string = string> {
40
- /**
41
- * Open a WebSocket subscription. Returns a {@link RealtimeSubscription} with the
42
- * connection `id` (same value the handler sees as `conn.id`) and an `unsubscribe()` method.
43
- *
44
- * Pass `options.id` to control the connection id (e.g. a stable per-tab id so a
45
- * reconnect reuses the same server-side connection); omit it for an auto-generated
46
- * per-connection id.
47
- */
48
- subscribe(instanceId: string, callback: (data: ToClientFor<N>) => void, options?: {
49
- id?: string;
50
- }): RealtimeSubscription;
51
- /** Send a message over the open socket. Throws if not subscribed. */
52
- send(instanceId: string, data: ToServerFor<N>): void;
53
- }
54
- /** Handle for an active realtime subscription. */
55
- export interface RealtimeSubscription {
56
- /** This connection's id — the same value the handler receives as `conn.id`. */
57
- id: string;
58
- /** Close the subscription and its underlying socket. */
59
- unsubscribe(): void;
60
- }
61
- /**
62
- * The realtime module provides access to Cloudflare Durable Object-backed
63
- * RealtimeHandlers deployed by the Base44 platform.
64
- *
65
- * Handler names are accessed as dynamic properties on this module:
66
- * ```typescript
67
- * const sub = await base44.realtime.MyHandler.subscribe("room-1", (msg) => {
68
- * console.log(msg); // typed if MyHandler is in RealtimeHandlerRegistry
69
- * });
70
- * const { id, unsubscribe } = sub;
71
- * unsubscribe();
72
- * ```
73
- */
74
- export type RealtimeModule = {
75
- [K in AllHandlerNames]: K extends keyof RealtimeHandlerRegistry ? RealtimeHandlerClient<string & K> : RealtimeHandlerClient;
76
- } & Record<string, RealtimeHandlerClient>;
77
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,86 +0,0 @@
1
- /**
2
- * Type-only base class for Realtime Handlers.
3
- *
4
- * Import and extend this in your handler files:
5
- * import { RealtimeHandler } from "@base44/sdk";
6
- * export class MyHandler extends RealtimeHandler { ... }
7
- *
8
- * At deploy time the bundler replaces this import with the compiled
9
- * Cloudflare Durable Object implementation — this file provides types only.
10
- */
11
- import type { Base44Client } from "./client.types.js";
12
- /**
13
- * A single client connection. `Send` is the message type this connection accepts
14
- * via {@link send} — the handler's *outgoing* (server→client) messages.
15
- */
16
- export interface Conn<Send = unknown> {
17
- /** Unique per-connection id (one per socket/tab), the same value the client
18
- * receives from `subscribe()`. Use this — not userId — to identify a distinct
19
- * client, so multiple tabs of the same user are separate connections. */
20
- id: string;
21
- userId: string;
22
- appId: string;
23
- instanceId: string;
24
- send(data: Send): void;
25
- reject(code: number, reason: string): void;
26
- }
27
- export interface Storage {
28
- get<T>(key: string): Promise<T | undefined>;
29
- put(key: string, value: unknown): Promise<void>;
30
- delete(key: string): Promise<boolean>;
31
- }
32
- /**
33
- * Base class for a Realtime Handler.
34
- *
35
- * @typeParam Incoming - messages this handler *receives* from clients
36
- * (`handleMessage`'s `msg`) — the schema's `toServer` section.
37
- * @typeParam Outgoing - messages this handler *sends* to clients
38
- * (`conn.send`/`broadcast`) — the schema's `toClient` section.
39
- *
40
- * With a generated `schema.jsonc`, wire both from the registry so they can't drift
41
- * from the client's types:
42
- * ```ts
43
- * type Reg = RealtimeHandlerRegistry["MyHandler"];
44
- * class MyHandler extends RealtimeHandler<Reg["toServer"], Reg["toClient"]> { ... }
45
- * ```
46
- */
47
- export declare abstract class RealtimeHandler<Incoming = unknown, Outgoing = unknown> {
48
- abstract handleConnect(conn: Conn<Outgoing>): void | Promise<void>;
49
- abstract handleMessage(conn: Conn<Outgoing>, msg: Incoming): void | Promise<void>;
50
- abstract handleClose(conn: Conn<Outgoing>): void | Promise<void>;
51
- abstract handleTick(): void | Promise<void>;
52
- onStart(): void | Promise<void>;
53
- /**
54
- * Managed ticker (opt-in). Override {@link shouldTick} and the platform runs
55
- * {@link handleTick} on a timer of {@link tickIntervalMs} while it returns true,
56
- * and stops (letting the Durable Object hibernate — no compute cost) when it
57
- * returns false. The platform owns scheduling, rescheduling, self-heal, and
58
- * error-safety — you don't call {@link startLoop}/{@link stopLoop}.
59
- *
60
- * Re-evaluated after every connect/message/close and on every tick, so keep it
61
- * cheap and pure (no async, no side effects). Example: `return this.players >= 2`.
62
- */
63
- protected tickIntervalMs: number;
64
- protected shouldTick?(): boolean;
65
- protected broadcast(_data: Outgoing): void;
66
- protected getConnections(): Conn<Outgoing>[];
67
- protected startLoop(_ms: number): Promise<void>;
68
- protected stopLoop(): Promise<void>;
69
- protected get instanceId(): string;
70
- protected get storage(): Storage;
71
- /**
72
- * SDK client acting **as the connected user** — every entity call respects
73
- * the app's row-level security, evaluated as that user at call time. The
74
- * default wherever a `conn` is in scope (connect/message/close).
75
- *
76
- * Throws if the connection carries no user credential (anonymous visitor,
77
- * signed-out session, or an app SDK that predates in-band auth).
78
- */
79
- protected createUserClient(conn: Conn): Base44Client;
80
- /**
81
- * Service-role SDK client — bypasses RLS. For work with **no user in scope**
82
- * (tick, alarm, onStart). Inside handleMessage/handleConnect prefer
83
- * `createUserClient(conn)`.
84
- */
85
- protected createServiceClient(): Base44Client;
86
- }
@@ -1,79 +0,0 @@
1
- /**
2
- * Type-only base class for Realtime Handlers.
3
- *
4
- * Import and extend this in your handler files:
5
- * import { RealtimeHandler } from "@base44/sdk";
6
- * export class MyHandler extends RealtimeHandler { ... }
7
- *
8
- * At deploy time the bundler replaces this import with the compiled
9
- * Cloudflare Durable Object implementation — this file provides types only.
10
- */
11
- /**
12
- * Base class for a Realtime Handler.
13
- *
14
- * @typeParam Incoming - messages this handler *receives* from clients
15
- * (`handleMessage`'s `msg`) — the schema's `toServer` section.
16
- * @typeParam Outgoing - messages this handler *sends* to clients
17
- * (`conn.send`/`broadcast`) — the schema's `toClient` section.
18
- *
19
- * With a generated `schema.jsonc`, wire both from the registry so they can't drift
20
- * from the client's types:
21
- * ```ts
22
- * type Reg = RealtimeHandlerRegistry["MyHandler"];
23
- * class MyHandler extends RealtimeHandler<Reg["toServer"], Reg["toClient"]> { ... }
24
- * ```
25
- */
26
- export class RealtimeHandler {
27
- constructor() {
28
- /**
29
- * Managed ticker (opt-in). Override {@link shouldTick} and the platform runs
30
- * {@link handleTick} on a timer of {@link tickIntervalMs} while it returns true,
31
- * and stops (letting the Durable Object hibernate — no compute cost) when it
32
- * returns false. The platform owns scheduling, rescheduling, self-heal, and
33
- * error-safety — you don't call {@link startLoop}/{@link stopLoop}.
34
- *
35
- * Re-evaluated after every connect/message/close and on every tick, so keep it
36
- * cheap and pure (no async, no side effects). Example: `return this.players >= 2`.
37
- */
38
- this.tickIntervalMs = 100;
39
- }
40
- onStart() { }
41
- broadcast(_data) {
42
- throw new Error("RealtimeHandler.broadcast() is only available inside a deployed handler");
43
- }
44
- getConnections() {
45
- throw new Error("RealtimeHandler.getConnections() is only available inside a deployed handler");
46
- }
47
- startLoop(_ms) {
48
- throw new Error("RealtimeHandler.startLoop() is only available inside a deployed handler");
49
- }
50
- stopLoop() {
51
- throw new Error("RealtimeHandler.stopLoop() is only available inside a deployed handler");
52
- }
53
- get instanceId() {
54
- throw new Error("RealtimeHandler.instanceId is only available inside a deployed handler");
55
- }
56
- get storage() {
57
- throw new Error("RealtimeHandler.storage is only available inside a deployed handler");
58
- }
59
- /**
60
- * SDK client acting **as the connected user** — every entity call respects
61
- * the app's row-level security, evaluated as that user at call time. The
62
- * default wherever a `conn` is in scope (connect/message/close).
63
- *
64
- * Throws if the connection carries no user credential (anonymous visitor,
65
- * signed-out session, or an app SDK that predates in-band auth).
66
- */
67
- createUserClient(conn) {
68
- void conn;
69
- throw new Error("RealtimeHandler.createUserClient() is only available inside a deployed handler");
70
- }
71
- /**
72
- * Service-role SDK client — bypasses RLS. For work with **no user in scope**
73
- * (tick, alarm, onStart). Inside handleMessage/handleConnect prefer
74
- * `createUserClient(conn)`.
75
- */
76
- createServiceClient() {
77
- throw new Error("RealtimeHandler.createServiceClient() is only available inside a deployed handler");
78
- }
79
- }