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/sync-client/scratch/primus.ts
Views: 687
/*1Create a very, very simple example using the nodejs ws library of a client2and server talking to each other via a websocket. The client and server3should both be nodejs processes (no web browser involved!).45The simple code below works fine and accomplishes the goal above.67In one node shell:89> require('./dist/primus').server();10Server Received message: hi1112In another:1314> socket = require('./dist/primus').client();15> Client Received message: Welcome to the WebSocket server!16> socket.write("hi")17undefined18*/1920import Primus from "primus";21import http from "http";22import { join } from "path";23import * as responder from "@cocalc/primus-responder";24import * as multiplex from "@cocalc/primus-multiplex";2526export function server() {27const httpServer = http.createServer((_req, res) => {28res.end("Hello from the server!");29});3031const primus = new Primus(httpServer, { transformer: "websockets" });3233primus.on("connection", (socket) => {34console.log("Client connected");35socket.write("Welcome to the server!");3637socket.on("data", (data) => {38console.log(`Received from client: ${data}`);39});40});4142httpServer.listen(8080, () => {43console.log("Server listening on port 8080");44});45}4647export function client() {48const primus = Primus.createSocket({ transformer: "websockets" });49const socket = new primus("http://localhost:8080");5051socket.on("open", () => {52console.log("Connected to server");53socket.write("Hello from the client!");5455socket.on("data", (data) => {56console.log(`Received from server: ${data}`);57});58});5960return socket;61}6263/*64connect to a specific project (bypassing the proxy) for now, so65we can flesh this out.6667> c = require('./dist/primus').project({appBasePath:'/10f0e544-313c-4efe-8718-2142ac97ad11/port/5000',project_id:'97ce5a7c-25c1-4059-8670-c7de96a0db92', port:34491})68> c.writeAndWait({cmd:'exec',opts:{command:'pwd'}}, console.log)69undefined70> {71stdout: '/home/user/cocalc/src/data/projects/97ce5a7c-25c1-4059-8670-c7de96a0db92\n',72stderr: '',73exit_code: 074}7576With this, we can make a proof of concept of a remote Jupyter77kernel. Then we have to worry about authentication.78*/7980export function project({81appBasePath,82project_id,83port,84}: {85appBasePath: string;86project_id: string;87port: number;88}) {89const url = `http://127.0.0.1:${port}`;90const opts = {91pathname: join(appBasePath, project_id, "raw/.smc/ws"),92transformer: "websockets",93plugin: { responder, multiplex },94} as const;95const primus = Primus.createSocket(opts);96const socket = new primus(url);9798socket.on("open", () => {99console.log("Connected to project");100socket.on("data", (data) => {101console.log(`Received from server: ${data}`);102});103});104105return socket;106}107108109