# Initalizing Server

## Content

This document explains how to initialize a collaboration server in `js-collaboration` using the [Server ](/spreadjs/api/v18/collaboration/js-collaboration/classes/Server)Class to enable bidirectional communication with clients.

## Installation

Install the required dependency package via npm:

```bash
npm install @mescius/js-collaboration
```

## Creating Server

* Standalone Usage

    ```typescript
    import { Server } from '@mescius/js-collaboration';
    const server = new Server({ port: 8080 });
    server.on('connect', (context) => {
        // ...
    });
    ```
* Use an HTTP Server

    ```typescript
    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

    ```typescript
    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

The parameter path is the request [path ](/spreadjs/api/v18/collaboration/js-collaboration/interfaces/IServerConfig#path)of the server. Its default is `"/collaboration/"`.

>type=warning
> The server and client values must match.

```typescript
import { Server } from '@mescius/js-collaboration';
const server = new Server({
    path: "/my-custom-path/"
});
```

```typescript
import { Client } from "@mescius/js-collaboration-client";
const client = new Client("ws://server-domain.com:8000", {
    path: "/my-custom-path/"
});
```

## Next Steps

After initializing the server, use the methods of the [Server ](/spreadjs/api/v18/collaboration/js-collaboration/classes/Server)class to register hooks and middleware for handling connections and messages. For more details, refer to the [Server Class](/spreadjs/docs/v18/spreadjs-collaboration-server/collaboration-framework/js-collaboration/server-class) and [Middleware](/spreadjs/docs/v18/spreadjs-collaboration-server/collaboration-framework/js-collaboration/server-middleware).