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/project/browser-websocket/server.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Create the Primus realtime socket server7*/89import { join } from "node:path";10import { Router } from "express";11import { Server } from "http";12import Primus from "primus";13import type { PrimusWithChannels } from "@cocalc/terminal";1415// We are NOT using UglifyJS because it can easily take 3 blocking seconds of cpu16// during project startup to save 100kb -- it just isn't worth it. Obviously, it17// would be optimal to build this one and for all into the project image. TODO.18//const UglifyJS = require("uglify-js");19import { init_websocket_api } from "./api";2021import { getLogger } from "@cocalc/project/logger";2223export default function init(server: Server, basePath: string): Router {24const winston = getLogger("websocket-server");25const opts = {26pathname: join(basePath, ".smc", "ws"),27transformer: "websockets",28} as const;29winston.info(`Initializing primus websocket server at "${opts.pathname}"...`);30const primus = new Primus(server, opts) as PrimusWithChannels;3132// add multiplex to Primus so we have channels.33primus.plugin("multiplex", require("@cocalc/primus-multiplex"));3435/* Add responder plugin, which adds a 'request' event to sparks,36spark.on("request", (data, done) => { done({'thanks for':data}) })37and38primus.writeAndWait({event:'foo'}, (response) => console.log("got", response))39See: https://github.com/swissmanu/primus-responder40*/41primus.plugin("responder", require("@cocalc/primus-responder"));4243init_websocket_api(primus);4445const router = Router();46const library: string = primus.library();47// See note above.48//UglifyJS.minify(primus.library()).code;4950router.get("/.smc/primus.js", (_, res) => {51winston.debug("serving up primus.js to a specific client");52res.send(library);53});54winston.info(55`waiting for clients to request primus.js (length=${library.length})...`,56);5758return router;59}606162