Skip to main content
This article describes how to configure and use WebSocket connections on a VPS or dedicated server.

About WebSocket connections

The WebSocket protocol enables you to establish persistent, two-way communication between a browser and a server over a single TCP socket. Web applications can then be responsive in real-time, without the need to poll a server for new information. Hosting.com supports WebSocket connections on the following plans:
  • VPS (managed and unmanaged).
  • Dedicated servers.
Hosting.com does not support WebSockets on shared or reseller hosting accounts at this time.
The following procedures guide you through the process of setting up and using WebSocket connections.

Step 1: Web server configuration

The first step is to enable WebSocket connections on the web server. How you do this depends on the type of account you have:
  • If you do not have root access to your server, our support team will configure the web server for you. Please open a support ticket at https://my.hosting.com and provide the following information:
    • The domain or URL you want to proxy for WebSocket connections.
    • The port number on which your WebSocket server application runs.
  • If you do have root access to your server, you can configure your web server for WebSocket connections independently. The exact steps to do this depend on the web server you are running:

Step 2: WebSocket server application

After you configure the web server to route WebSocket connections, you are ready to run a WebSocket server application that accepts and processes those connections. You can write this application using any programming language, but the most common implementations are in Python or Node.js. Functionally-equivalent code samples for these two languages follow.
ImportantThese code samples are for proof-of-concept purposes, and are not intended for production use. You should make sure your own code is suitable for a production environment.

Python server code sample

To run WebSockets in Python, use the websockets library.
You should run a newer Python version on the server (at least Python 3.9). If the server’s package repositories only have older Python versions, you can download and compile Python yourself. For information about how to do this, please see Configuring and using a newer version of Python.
To set up a Python virtual environment, install the websockets library, and create a server application, follow these steps:
  1. Log in to the server using SSH.
  2. At the command prompt, type the following commands:
  3. To create a basic WebSockets server that runs on port 5678, copy the following code, and then paste it into a file named server.py:
  4. Within the virtual environment, type the following command to start the server:
    The WebSocket server application is now running, and you are ready to set up the WebSocket client.

Node.js server code sample

To run WebSockets in Node.js, use the ws library.
You should run a newer Node.js version on the server (at least Node.js 16). If the server’s package repositories only have older Node.js versions, you can download and install a newer Node.js version yourself. For information about how to do this, please see Installing and configuring Node.js on managed hosting.
To install the ws library and create a server application, follow these steps:
  1. Log in to the server using SSH.
  2. At the command prompt, type the following command:
  3. To create a basic WebSockets server that runs on port 5678, copy the following code, and then paste it into a file named server.mjs:
    🚧 Important Make sure you save the file with an .mjs extension, not .js.
  4. Type the following command to start the server:
    The WebSocket server application is now running, and you are ready to set up the WebSocket client.

Step 3: WebSocket client

At this point, the web server is configured to tunnel WebSocket connections, and you have a running WebSocket server. You are now ready to set up and test WebSocket connections from a web browser.

Client web page code sample

This basic web page contains a JavaScript function that opens a secure WebSocket connection and sends some data to the server, which the server then echoes back to the client. In line 9 (let socket = …), make sure you replace example.com with your own server’s domain name (or IP address):
Save this page on the server, and then load it in your web browser. You should receive popup messages about the connection, and the client message “Hello from WebSockets” echoed back to you from the server. You now have a fully functioning WebSockets application!

Step 4 (optional): Make the server application persistent

In most cases, you will want your WebSocket server application to start automatically after the server reboots or if the application crashes. There are several ways to do this, but if you have root access to the server, a systemd service is quick and easy to set up. To do this, follow these steps:
  1. Using your preferred text editor, in the /etc/systemd/system directory, create a file named websocket-server.service.
  2. Copy one of the following text blocks, and then paste it into the websocket-server.service file. The first text block demonstrates how to start the Python-based WebSocket server application. The second text block demonstrates how to start the Node.js-based WebSocket server application. Update the paths and filenames in the ExecStart line as needed for your own configuration. Starting a Python-based application:
    Starting a Node.js-based application:
  3. Save your changes to the websocket-server.service file.
  4. At the command prompt, type the following commands:
  5. To confirm the service started correctly, type the following command:
  6. To enable the service to start automatically after the server is rebooted, type the following command: