Rob's web

Websocket

WebSocket is a computer communications protocol, providing a bidirectional communication channel over a single Transmission Control Protocol (TCP) connection. The protocol was standardized by the IETF as RFC 6455 in 2011. The current specification allowing web applications to use this protocol is known as WebSockets. It is a living standard maintained by the WHATWG and a successor to The WebSocket API from the W3C.

Apache Server was originally designed for stateless HTTP requests, where each request is processed independently and connections are short-lived. WebSockets, however, require persistent connections that remain open for extended periods to enable real-time data exchange.

Apache webSocket implementation options

Several approaches exist for implementing WebSockets with Apache Server

Using mod_websocket Module

The mod_websocket module extends Apache to handle WebSocket connections directly. This module intercepts WebSocket handshake requests and manages the persistent connections.

We start with checking for the needed modules:

If you made the private webserver you can use apache to show the installed modules. Go to https://prive.example.com/server-info.

Other option is to look in the config directorie.

# cd /etc/httpd/conf.modules.d
# ll
total 56
-rw-r--r--. 1 root root 3325 Dec 23 05:29 00-base.conf
-rw-r--r--. 1 root root   47 Dec 23 05:29 00-brotli.conf
-rw-r--r--. 1 root root  139 Dec 23 05:29 00-dav.conf
-rw-r--r--. 1 root root   41 Dec 23 05:29 00-lua.conf
-rw-r--r--. 1 root root  948 Dec 23 05:29 00-mpm.conf
-rw-r--r--. 1 root root  845 Dec 23 05:29 00-optional.conf
-rw-r--r--. 1 root root 1073 Dec 23 05:29 00-proxy.conf
-rw-r--r--. 1 root root   41 Dec 23 05:29 00-ssl.conf
-rw-r--r--. 1 root root   88 Dec 23 05:29 00-systemd.conf
-rw-r--r--. 1 root root  367 Dec 23 05:29 01-cgi.conf
-rw-r--r--. 1 root root   45 Nov  6  2025 10-h2.conf
-rw-r--r--. 1 root root   57 Nov  6  2025 10-proxy_h2.conf
-rw-r--r--. 1 root root  347 May  6 07:50 20-php.conf

# cat 00-proxy.conf
# This file configures all the proxy modules:
LoadModule proxy_module modules/mod_proxy.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_express_module modules/mod_proxy_express.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_hcheck_module modules/mod_proxy_hcheck.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

Adding dns entry

# cd /var/named/dynamic/
# vi example.com
ws                      CNAME   server5
www                     CNAME   server5
# systemctl restart named
# ping ws.example.com
#

Installing node.js

/* Download and install nvm */
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

/* In lieu of restarting the shell */
# \. "$HOME/.nvm/nvm.sh"

/* Install node.js */
# nvm install 26
.....
# node -v
v26.1.0
# npm -v
11.13.0

Node.js is ready for use. Install ws:

# npm install ws
# npm install bufferutil --save-optional

Creating a virtual host

We create first the server space and then we create an configuration file for the virtual host.

# cd /srv/www/vhosts/
# mkdir -p ws.example.com/httpsdocs/
# chcon -R -t httpd_sys_rw_content_t httpsdocs
# cd /etc/http/conf/vhosts.d/
# cp vhosts.con ws.example.com.conf

We need first to change the template for the new virtual host. Start with the URL. Change www in ws.

# vi ws.example.com.conf


# httpd -t
# systemctl restart httpd
# 
<Location "/websocket">
    SetHandler websocket-handler
    WebSocketHandler /path/to/websocket/script.py
</Location>

Links