[]
        
(Showing Draft Content)

サーバーの初期化

このドキュメントでは、Server クラスを使用して js-collaboration でコラボレーションサーバーを初期化し、クライアントとの双方向通信を有効化する方法を説明します。

初期化

npm で必要な依存パッケージをインストールします。

npm install @mescius/js-collaboration

サーバーの作成

  • スタンドアロンで使用する場合

    import { Server } from '@mescius/js-collaboration';
    const server = new Server({ port: 8080 });
    server.on('connect', (context) => {
        // ...
    });
  • HTTP サーバーを使用する場合

    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);
  • 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 });
    // Express ミドルウェアを登録します
    app.use(express.static('public'));
    // 例として、Express のエンドポイントまたはルートを登録します
    app.get('/recent-files', (req, res) => {
        res.send(['file1', 'file2']);
    });
    // 例として、コラボレーションのフックを登録します
    server.on('connect', (context) => {
        // ...
    });
    httpServer.listen(8080);

インターフェイス: IServerConfig

path

パラメータ path は、サーバーのリクエストパスです。既定値は "/collaboration/" です。

サーバーとクライアントの値は一致させる必要があります。

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

マルチノードまたは負荷分散(ロードバランシング)環境向けの、ユーザー定義の Socket.IO のカスタムアダプター です。複数のサーバーインスタンス間でメッセージをブロードキャストするために、さまざまなメッセージブローカー(Redis、MongoDB、PostgreSQL など)と統合できます。

例

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

// 1. Pub/Sub クライアントを初期化する
const pubClient = createClient({ url: 'redis://localhost:6379' });
const subClient = pubClient.duplicate();
await Promise.all([pubClient.connect(), subClient.connect()]);

// 2. Adapter を注入する
const server = new Server({
    port: 8080,
    socketIoAdapter: createAdapter(pubClient, subClient)
});

次のステップ

サーバーを初期化した後、Server クラスのメソッドを使用して、接続とメッセージを処理するためのフックおよびミドルウェアを登録します。詳細は、「Server クラス」と「サーバーミドルウェア」を参照してください。