[]
        
(Showing Draft Content)

Initalizing DocumentServices

This document explains how to initialize DocumentServices on the server side, including:

  • Install dependencies

  • Create a server instance

  • Configure OT functionality

Installation

Install the required dependency packages via npm:

npm install @mescius/js-collaboration @mescius/js-collaboration-ot
  • js-collaboration: Provides basic server functionality and the Server class.

  • js-collaboration-ot: Provides OT functionality, including documentFeature and OT type management.

Initialize the Server

  1. Register the OT Type

    Before starting the server, you need to register the OT Type to define operation behavior. Here is an example of registering a simple text type:

    import { TypesManager } from '@mescius/js-collaboration-ot';
    
    const textType = {
        uri: 'rich-text-ot-type'
        create: (data) => data || '',
        transform: (op1, op2, side) => {
            if (op1.pos < op2.pos) return op1;
            return { pos: op1.pos + op2.text.length, text: op1.text };
        },
        apply: (snapshot, op) => {
            return snapshot.slice(0, op.pos) + op.text + snapshot.slice(op.pos);
        }
    };
    
    TypesManager.register(textType);
  2. Create a Server Instance

    Use the Server class from js-collaboration to create a server instance.

    import { Server } from '@mescius/js-collaboration';
    
    const server = new Server({ port: 8080 });

    Role: Initializes the server and listens for WebSocket connections.

  3. Integrate OT Functionality

    Use the documentFeature from js-collaboration-ot to add OT functionality to the server.

    import { documentFeature } from '@mescius/js-collaboration-ot';
    
    // Integrate OT functionality
    server.useFeature(documentFeature());

    Parameter

    service (optional): A DocumentServices instance, used for custom database and configuration (see Advanced Configuration).

    Role: Enables OT document feature, allowing the server to handle operations and snapshots.

Complete Example

Below is a complete example of server initialization:

import { Server } from '@mescius/js-collaboration';
import { documentFeature, TypesManager } from '@mescius/js-collaboration-ot';

// Define the OT Type
const textType = {
    uri: 'text',
    create: (data) => data || '',
    transform: (op1, op2, side) => {
        if (op1.pos < op2.pos) return op1;
        return { pos: op1.pos + op2.text.length, text: op1.text };
    },
    apply: (snapshot, op) => {
        return snapshot.slice(0, op.pos) + op.text + snapshot.slice(op.pos);
    }
};
// Register the OT Type
TypesManager.register(textType);

// Create the server
const server = new Server({ port: 8080 });

// Integrate OT functionality
server.useFeature(documentFeature());

// Optional: Listen for connection events
server.on('connect', ({ connection }) => {
    console.log(`Client ${connection.id} has connected`);
});

Advanced Configuration

If you need to customize OT behavior or data storage, configure DocumentServices and pass it to documentFeature.

import { documentFeature, DocumentServices, MemoryDb } from '@mescius/js-collaboration-ot';

// Configure DocumentServices
const docService = new DocumentServices({
    db: new MemoryDb(), // Use in-memory database
    submitSnapshotBatchSize: 50 // Snapshot submission batch size
});

// Integrate OT functionality
server.useFeature(documentFeature(docService));

Parameters (IDocConfig):

  • db: Database adapter (defaults to MemoryDb).

  • maxSubmitRetries: Maximum number of retries for operation submission (default is unlimited).

  • submitSnapshotBatchSize: Snapshot submission batch size (default is 100).

Next Steps

Constructing and Using DocumentServices