Deploy y-websocket Server Using Docker

There are multiple y-websocket compatible backends for y-websocket.

This guide will show you how to deploy a y-websocket server using Docker.

By using either @y/websocket-server or hocuspocus, you can set up a collaborative editing backend tailored to your needs.

@y/websocket-server

The @y/websocket-server is an official implementation of a y-websocket server. It is lightweight and easy to set up.

Official Documentation

https://github.com/yjs/y-websocket-server.

Method 1: Deploy Using Docker

Below is a sample Dockerfile to deploy the @y/websocket-server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM node:16-alpine

# Set the working directory
WORKDIR /app

# Configure npm registry
RUN npm config set registry https://registry.npmmirror.com/

# Install y-websocket server
RUN npm i @y/websocket-server

# Set environment variables
ENV HOST=0.0.0.0 PORT=1234

# Expose the default y-websocket port
EXPOSE 1234

# Start the y-websocket server
CMD ["npx", "y-websocket"]

To build and run the Docker container:

  1. Save the above Dockerfile in a directory.
  2. Build the Docker image:
    1
    docker build -t y-websocket-server .
  3. Run the container:
    1
    docker run -p 1234:1234 y-websocket-server

Your y-websocket server will now be accessible on port 1234.

Method 2: Direct Use

If you prefer not to use Docker, you can install and run the server directly on your local machine:

1
2
npm install @y/websocket-server
npx y-websocket

This will start the server on the default port 1234.


hocuspocus

hocuspocus is another y-websocket-compatible backend.

It provides additional features like authentication, awareness updates, and persistence.

Official Documentation

You can find the official documentation here: hocuspocus GitHub Repository.

Method 1: Deploy Using Docker

Below is a sample Dockerfile to deploy hocuspocus:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM node:16-alpine

# Set the working directory
WORKDIR /app

# Configure npm registry
RUN npm config set registry https://registry.npmmirror.com/

# Install hocuspocus
RUN npm i @hocuspocus/server

# Set environment variables
ENV HOST=0.0.0.0 PORT=1234

# Expose the default hocuspocus port
EXPOSE 1234

# Start the hocuspocus server
CMD ["node", "-e", "require('@hocuspocus/server').Server.configure({ port: process.env.PORT }).listen()"]

To build and run the Docker container:

  1. Save the above Dockerfile in a directory.
  2. Build the Docker image:
    1
    docker build -t hocuspocus-server .
  3. Run the container:
    1
    docker run -p 1234:1234 hocuspocus-server

Your hocuspocus server will now be accessible on port 1234.

Method 2: Direct Use

If you prefer not to use Docker, you can install and run the server directly on your local machine:

1
2
npm install @hocuspocus/server
node -e "require('@hocuspocus/server').Server.configure({ port: 1234 }).listen()"

This will start the server on the default port 1234.


Deploy y-websocket Server Using Docker
https://www.hardyhu.cn/2024/12/17/Deploy-y-websocket-server-using-Docker/
Author
John Doe
Posted on
December 17, 2024
Licensed under