[]
This document explains how to initialize a collaboration server in js-collaboration
using the Server Class to enable bidirectional communication with clients.
Install the required dependency package via npm:
npm install @mescius/js-collaboration
Standalone Usage
import { Server } from '@mescius/js-collaboration';
const server = new Server({ port: 8080 });
server.on('connect', (context) => {
// ...
});
Use an HTTP Server
import { createServer } from 'http';
import { Server } from '@mescius/js-collaboration';
const httpServer = createServer();
const server = new Server({ httpServer });
server.on('connect', (context) => {
// ...
});
httpServer.listen(8080);
Use Express
import express from "express";
import { createServer } from "http";
import { Server } from "@mescius/js-collaboration";
const app = express();
const httpServer = createServer(app);
const server = new Server({ httpServer });
// Register Express middleware
app.use(express.static('public'));
// Register Express endpoint or route, for example:
app.get('/recent-files', (req, res) => {
res.send(['file1', 'file2']);
});
// Register collaboration hook, for example:
server.on('connect', (context) => {
// ...
});
httpServer.listen(8080);
The parameter path is the request path of the server. Its default is "/collaboration/"
.
type=warning
The server and client values must match.
import { Server } from '@mescius/js-collaboration';
const server = new Server({
path: "/my-custom-path/"
});
import { Client } from "@mescius/js-collaboration-client";
const client = new Client("ws://server-domain.com:8000", {
path: "/my-custom-path/"
});
After initializing the server, use the methods of the Server class to register hooks and middleware for handling connections and messages. For more details, refer to the Server Class and Middleware.