[]
The Server class from @mescius/js-collaboration initializes the server-side endpoint for real-time collaboration.
This page explains how to create and configure a Server instance.
Install the server package:
npm install @mescius/js-collaborationThe simplest way to start the server is by specifying a port.
import { Server } from '@mescius/js-collaboration';
const server = new Server({ port: 8080 });
server.on('connect', (context) => {
// Handle client connection
});Use this mode when the collaboration service runs independently.
If your application already uses a Node.js server (for example, Express), you can attach the collaboration server to it.
Supported server types:
http.Server
https.Server
http2.Http2Server
http2.Http2SecureServer
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 });
app.use(express.static("public"));
server.on("connect", (context) => {
// Collaboration logic
});
httpServer.listen(8080);Use this mode when:
Sharing the same server with REST APIs
Using existing middleware
Deploying in a unified backend service
By default, the collaboration endpoint uses: /collaboration/
The server and client configurations must use the same
path.
Server
const server = new Server({
port: 8080,
path: "/my-custom-path/"
});Client
import { Client } from "@mescius/js-collaboration-client";
const client = new Client("ws://server-domain.com:8000", {
path: "/my-custom-path/"
});In addition to port, httpServer, and path, the Server constructor accepts advanced configuration options through IServerConfig.
These options allow you to:
Configure cross-origin access (CORS)
Validate handshake or upgrade requests
Control maximum payload size
Customize the Socket.IO adapter for distributed deployments
The following example illustrates how multiple advanced options can be combined in a production-style configuration. Replace the placeholder values with settings appropriate for your environment.
import { Server } from '@mescius/js-collaboration';
import { createClient } from 'redis';
import { createAdapter } from '@socket.io/redis-adapter';
const pubClient = createClient({ url: 'redis://localhost:6379' });
const subClient = pubClient.duplicate();
await Promise.all([pubClient.connect(), subClient.connect()]);
const server = new Server({
port: 8080,
// Custom endpoint
path: "/collaboration/",
// Enable multi-node broadcasting
socketIoAdapter: createAdapter(pubClient, subClient),
// Configure CORS
cors: {
origin: ["https://app.example.com"]
},
// Restrict handshake requests
allowRequest: (req, callback) => {
const origin = req.headers.origin;
callback(null, !!origin);
},
// Control payload limits
maxHttpBufferSize: 1024 * 1024 * 1024,
maxDecompressedMessageSize: 1024 * 1024 * 1024,
});For multi-node deployments (for example, behind a load balancer), configure a custom adapter to synchronize messages across instances.
See:
For complete configuration options, see:
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.