Skip to content

Commit 1f84fb0

Browse files
thomasballingerConvex, Inc.
authored andcommitted
onServerDisconnectError callback (#37850)
Stopgap client callback to report unusual server disconnect errors. This would be an ugly API to be stuck with for the long term because it leaks details that are irrelevant, but customers have requested a way to know on their users' clients that the connection is down so we're getting this out quickly. The content of these messages, which messages trigger the callback, and the existence of the callback are all subject to change. GitOrigin-RevId: 86c37e76ee0517f1dee54273ec6ac129021371d2
1 parent 57027e6 commit 1f84fb0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/browser/sync/client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ export interface BaseConvexClientOptions {
8383
* The default value is `false`.
8484
*/
8585
reportDebugInfoToConvex?: boolean;
86+
/**
87+
* This API is experimental: it may change or disappear.
88+
*
89+
* A function to call on receiving abnormal WebSocket close messages from the
90+
* connected Convex deployment. The content of these messages is not stable,
91+
* it is an implementation detail that may change.
92+
*
93+
* Consider this API an observability stopgap until higher level codes with
94+
* recommendations on what to do are available, which could be a more stable
95+
* interface instead of `string`.
96+
*
97+
* Check `connectionState` for more quantitative metrics about connection status.
98+
*/
99+
onServerDisconnectError?: (message: string) => void;
86100
/**
87101
* Skip validating that the Convex deployment URL looks like
88102
* `https://happy-animal-123.convex.cloud` or localhost.
@@ -450,6 +464,7 @@ export class BaseConvexClient {
450464
hasSyncedPastLastReconnect: this.hasSyncedPastLastReconnect(),
451465
};
452466
},
467+
onServerDisconnectError: options.onServerDisconnectError,
453468
},
454469
webSocketConstructor,
455470
this.logger,

src/browser/sync/web_socket_manager.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,17 @@ export class WebSocketManager {
167167
private readonly onMessage: (message: ServerMessage) => OnMessageResponse;
168168
private readonly webSocketConstructor: typeof WebSocket;
169169
private readonly logger: Logger;
170+
private readonly onServerDisconnectError:
171+
| ((message: string) => void)
172+
| undefined;
170173

171174
constructor(
172175
uri: string,
173176
callbacks: {
174177
onOpen: (reconnectMetadata: ReconnectMetadata) => void;
175178
onResume: () => void;
176179
onMessage: (message: ServerMessage) => OnMessageResponse;
180+
onServerDisconnectError?: (message: string) => void;
177181
},
178182
webSocketConstructor: typeof WebSocket,
179183
logger: Logger,
@@ -195,6 +199,7 @@ export class WebSocketManager {
195199
this.onOpen = callbacks.onOpen;
196200
this.onResume = callbacks.onResume;
197201
this.onMessage = callbacks.onMessage;
202+
this.onServerDisconnectError = callbacks.onServerDisconnectError;
198203
this.logger = logger;
199204

200205
this.connect();
@@ -293,6 +298,12 @@ export class WebSocketManager {
293298
msg += `: ${event.reason}`;
294299
}
295300
this.logger.log(msg);
301+
if (this.onServerDisconnectError) {
302+
// This callback is a unstable API, InternalServerErrors may be removed in the future
303+
// since they reflect a expected temporary outage. But until a quantitative measure
304+
// of uptime is reported this unstable API errs on the inclusive side.
305+
this.onServerDisconnectError(msg);
306+
}
296307
}
297308
const reason = classifyDisconnectError(event.reason);
298309
this.scheduleReconnect(reason);

0 commit comments

Comments
 (0)