Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/hub/servers/primus.ts
Views: 687
import { join } from "path";1import { Router } from "express";2import Primus from "primus";3import base_path from "@cocalc/backend/base-path";4import Logger from "@cocalc/backend/logger";5import setup_primus_client from "@cocalc/hub/primus-client";6const { Client } = require("@cocalc/hub/client");7import { len } from "@cocalc/util/misc";8import { database } from "./database";910interface Options {11httpServer;12router: Router;13projectControl;14clients: { [id: string]: any }; // todo: when client is in typescript, use proper type...15host: string;16port: number;17isPersonal: boolean;18}1920export default function init({21httpServer,22router,23projectControl,24clients,25host,26port,27isPersonal,28}: Options): void {29const logger = Logger("primus");3031// It is now safe to change the primusOpts below, and this32// doesn't require changing anything anywhere else.33// See https://github.com/primus/primus#getting-started34const primusOpts = {35pathname: join(base_path, "hub"),36maxLength: 2 * 10485760, // 20MB - twice the default37compression: true,38} as const;39const primus_server = new Primus(httpServer, primusOpts);40logger.info(`listening on ${primusOpts.pathname}`);4142// Make it so new websocket connection requests get handled:43primus_server.on("connection", function (conn) {44// Now handle the connection45logger.info(`new connection from ${conn.address.ip} -- ${conn.id}`);46clients[conn.id] = new Client({47conn,48logger,49database,50projectControl,51host,52port,53personal: isPersonal,54});55logger.info(`num_clients=${len(clients)}`);56});5758// Serve the primus.js client code via the express router.59setup_primus_client(router, primus_server);60}616263