CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/servers/primus.ts
Views: 687
1
import { join } from "path";
2
import { Router } from "express";
3
import Primus from "primus";
4
import base_path from "@cocalc/backend/base-path";
5
import Logger from "@cocalc/backend/logger";
6
import setup_primus_client from "@cocalc/hub/primus-client";
7
const { Client } = require("@cocalc/hub/client");
8
import { len } from "@cocalc/util/misc";
9
import { database } from "./database";
10
11
interface Options {
12
httpServer;
13
router: Router;
14
projectControl;
15
clients: { [id: string]: any }; // todo: when client is in typescript, use proper type...
16
host: string;
17
port: number;
18
isPersonal: boolean;
19
}
20
21
export default function init({
22
httpServer,
23
router,
24
projectControl,
25
clients,
26
host,
27
port,
28
isPersonal,
29
}: Options): void {
30
const logger = Logger("primus");
31
32
// It is now safe to change the primusOpts below, and this
33
// doesn't require changing anything anywhere else.
34
// See https://github.com/primus/primus#getting-started
35
const primusOpts = {
36
pathname: join(base_path, "hub"),
37
maxLength: 2 * 10485760, // 20MB - twice the default
38
compression: true,
39
} as const;
40
const primus_server = new Primus(httpServer, primusOpts);
41
logger.info(`listening on ${primusOpts.pathname}`);
42
43
// Make it so new websocket connection requests get handled:
44
primus_server.on("connection", function (conn) {
45
// Now handle the connection
46
logger.info(`new connection from ${conn.address.ip} -- ${conn.id}`);
47
clients[conn.id] = new Client({
48
conn,
49
logger,
50
database,
51
projectControl,
52
host,
53
port,
54
personal: isPersonal,
55
});
56
logger.info(`num_clients=${len(clients)}`);
57
});
58
59
// Serve the primus.js client code via the express router.
60
setup_primus_client(router, primus_server);
61
}
62
63