Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/socket/util.ts
1453 views
1
export const SOCKET_HEADER_CMD = "CN-SocketCmd";
2
export const SOCKET_HEADER_SEQ = "CN-SocketSeq";
3
4
export type State = "disconnected" | "connecting" | "ready" | "closed";
5
6
export type Role = "client" | "server";
7
8
// client pings server this frequently and disconnects if
9
// doesn't get a pong back. Server disconnects client if
10
// it doesn't get a ping as well. This is NOT the primary
11
// keep alive/disconnect mechanism -- it's just a backup.
12
// Primarily we watch the connect/disconnect events from
13
// socketio and use those to manage things. This ping
14
// is entirely a "just in case" backup if some event
15
// were missed (e.g., a kill -9'd process...)
16
export const PING_PONG_INTERVAL = 60000;
17
18
// We queue up unsent writes, but only up to a point (to not have a huge memory issue).
19
// Any write beyond this size result in an exception.
20
// NOTE: in nodejs the default for exactly this is "infinite=use up all RAM", so
21
// maybe we should make this even larger (?).
22
// Also note that this is just the *number* of messages, and a message can have
23
// any size.
24
export const DEFAULT_MAX_QUEUE_SIZE = 1000;
25
26
export const DEFAULT_COMMAND_TIMEOUT = 3000;
27
28
export const DEFAULT_KEEP_ALIVE = 90000;
29
export const DEFAULT_KEEP_ALIVE_TIMEOUT = 15000;
30
31
export type Command = "connect" | "close" | "ping" | "socket";
32
33
import { type Client } from "@cocalc/conat/core/client";
34
35
export interface SocketConfiguration {
36
maxQueueSize?: number;
37
// (Default: true) Whether reconnection is enabled or not.
38
// If set to false, you need to manually reconnect:
39
reconnection?: boolean;
40
// ping other end of the socket if no data is received for keepAlive ms;
41
// if other side doesn't respond within keepAliveTimeout, then the
42
// connection switches to the 'disconnected' state.
43
keepAlive?: number; // default: DEFAULT_KEEP_ALIVE
44
keepAliveTimeout?: number; // default: DEFAULT_KEEP_ALIVE_TIMEOUT}
45
// desc = optional, purely for admin/user
46
desc?: string;
47
}
48
49
export interface ConatSocketOptions extends SocketConfiguration {
50
subject: string;
51
client: Client;
52
role: Role;
53
id: string;
54
}
55
56
export const RECONNECT_DELAY = 500;
57
58
export function clientSubject(subject: string) {
59
const segments = subject.split(".");
60
segments[segments.length - 2] = "client";
61
return segments.join(".");
62
}
63
64