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.

LoadModule websocket_module modules/mod_websocket.so

<Location "/websocket">
    SetHandler websocket-handler
    WebSocketHandler /path/to/websocket/script.py
</Location>

Adding dns entry

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

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
#