[]
        
(Showing Draft Content)

Initalizing Server

This document explains how to initialize a collaboration server in js-collaboration using the Server Class to enable bidirectional communication with clients.

Installation

Install the required dependency package via npm:

npm install @mescius/js-collaboration

Creating Server

  • 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);

Interface: IServerConfig

path

The parameter path is the request path of the server. Its default is "/collaboration/".

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/"
});

socketIoAdapter

Custom Adapter | Socket.IO adapter for multi-node or distributed deployments

Enables integration with various message brokers (Redis, MongoDB, PostgreSQL, etc.) for message broadcasting across multiple server instances.

Example

import { Server } from '@mescius/js-collaboration';
import { createClient } from 'redis';
import { createAdapter } from '@socket.io/redis-adapter';

// 1. Initialize Pub/Sub client
const pubClient = createClient({ url: 'redis://localhost:6379' });
const subClient = pubClient.duplicate();
await Promise.all([pubClient.connect(), subClient.connect()]);

// 2. Inject Adapter
const server = new Server({
    port: 8080,
    socketIoAdapter: createAdapter(pubClient, subClient)
});

Next Steps

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.