[]
이 문서는 js-collaboration에서 Server 클래스를 사용하여 클라이언트와의 양방향 통신을 활성화하는 동시 작업 서버를 초기화하는 방법을 설명합니다.
npm을 통해 필요한 의존성 패키지를 설치합니다.
npm install @mescius/js-collaborationimport { Server } from '@mescius/js-collaboration';
const server = new Server({ port: 8080 });
server.on('connect', (context) => {
// ...
});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);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);path 매개변수는 서버의 요청 경로(Request Path)를 지정합니다.
기본값은 "/collaboration/"입니다.
서버와 클라이언트의
path값은 반드시 동일해야 합니다.
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/"
});멀티 노드 또는 분산 환경 배포를 위한 커스텀 Socket.IO Adapter 입니다.
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 클래스의 메서드를 사용하여
연결(Connection) 및 메시지(Message)를 처리하기 위한 훅(Hook) 과 미들웨어(Middleware) 를 등록할 수 있습니다.
자세한 내용은 다음 문서를 참고하세요.