Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/socket/util.ts
1710 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 = 90000;
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 let DEFAULT_COMMAND_TIMEOUT = 10_000;
27
export let DEFAULT_KEEP_ALIVE = 25_000;
28
export let DEFAULT_KEEP_ALIVE_TIMEOUT = 10_000;
29
30
export function setDefaultSocketTimeouts({
31
command = DEFAULT_COMMAND_TIMEOUT,
32
keepAlive = DEFAULT_KEEP_ALIVE,
33
keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT,
34
}: {
35
command?: number;
36
keepAlive?: number;
37
keepAliveTimeout?: number;
38
}) {
39
DEFAULT_COMMAND_TIMEOUT = command;
40
DEFAULT_KEEP_ALIVE = keepAlive;
41
DEFAULT_KEEP_ALIVE_TIMEOUT = keepAliveTimeout;
42
}
43
44
export type Command = "connect" | "close" | "ping" | "socket";
45
46
import { type Client } from "@cocalc/conat/core/client";
47
48
export interface SocketConfiguration {
49
maxQueueSize?: number;
50
// (Default: true) Whether reconnection is enabled or not.
51
// If set to false, you need to manually reconnect:
52
reconnection?: boolean;
53
// ping other end of the socket if no data is received for keepAlive ms;
54
// if other side doesn't respond within keepAliveTimeout, then the
55
// connection switches to the 'disconnected' state.
56
keepAlive?: number; // default: DEFAULT_KEEP_ALIVE
57
keepAliveTimeout?: number; // default: DEFAULT_KEEP_ALIVE_TIMEOUT}
58
// desc = optional, purely for admin/user
59
desc?: string;
60
}
61
62
export interface ConatSocketOptions extends SocketConfiguration {
63
subject: string;
64
client: Client;
65
role: Role;
66
id: string;
67
}
68
69
export const RECONNECT_DELAY = 500;
70
71
export function clientSubject(subject: string) {
72
const segments = subject.split(".");
73
segments[segments.length - 2] = "client";
74
return segments.join(".");
75
}
76
77