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.
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:
- For information about how to configure Apache for WebSockets, please see https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html.
- For information about how to configure Nginx for WebSockets, please see https://nginx.org/en/docs/http/websocket.html.
- For information about how to configure other web servers for WebSockets, please see the relevant documentation for that server.
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.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.
- Log in to the server using SSH.
-
At the command prompt, type the following commands:
-
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:
-
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.
- Log in to the server using SSH.
-
At the command prompt, type the following command:
-
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.
-
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):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:- Using your preferred text editor, in the /etc/systemd/system directory, create a file named websocket-server.service.
-
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:
- Save your changes to the websocket-server.service file.
-
At the command prompt, type the following commands:
-
To confirm the service started correctly, type the following command:
-
To enable the service to start automatically after the server is rebooted, type the following command: