Implementing Apache Web Socket on FreeBSD server

Before we begin, it is important to understand that WebSocket is a protocol that runs on top of TCP. Typically, a WebSocket server is implemented as a separate application or module that processes incoming WebSocket requests and establishes a connection to the client.

In this tutorial, we'll cover the basics of working with WebSocket on an Apache server and show you how to create a simple Apache server by using WebSocket in the programming language of your choice. We'll also learn various aspects of implementing WebSocket on an Apache server, including establishing connections, exchanging messages, and terminating connections.


1. System specifications

OS: FreeBSD 13.2
Apache version: apache24-2.4.58_1
IP Apache: 192.168.5.2
Port Apache: 8080
Jenkins version: jenkins-2.428 (backend)
IP Jenkins: 192.168.5.2
Port Jenkins: 8180


2. Introduction Apache Websocket

The Apache WebSocket module is an Apache 2.x server module that can be used to process requests from client computers using the WebSocket protocol (RFC 6455) to the Apache 2.x server. This module consists of a plugin architecture to handle WebSocket messaging. Doing this does not require any knowledge of Apache's internal structure.

WebSocket is a new technology that allows you to create a permanent two-way connection between a web browser and an Apache server. It is a great alternative to traditional HTTP requests and allows you to create interactive applications in real-time between the client and server and eliminates the need for constant HTTP requests. Apache WebSocket Server is a popular implementation of WebSocket technology that enables developers to build efficient, scalable, and responsive web applications.

The Apache WebSocket server works by creating a persistent connection between the client and the server. This means the client and server can send data to each other at any time, without requiring a new HTTP request. WebSocket technology is very useful for real-time applications, such as stock tickers, live chat, games, and more.

Features Apache WebSocket:

1. Cross-Platform Compatibility
Apache WebSocket server is compatible with a variety of platforms and programming languages, including Java, PHP, Python, and more. This makes it easy for developers to integrate WebSocket technology into their existing projects.

2. Scalability and Load Balancing
Apache WebSocket server is designed to be scalable and can handle a large number of concurrent connections. It also supports load balancing, which ensures that incoming traffic is evenly distributed among multiple servers.

3. Security and Authentication
Apache WebSocket server supports secure connections using SSL/TLS encryption. It also offers authentication mechanisms to ensure that only authorized clients can access the server.

4. Real-Time Messaging and Broadcasting
Apache WebSocket server allows developers to build real-time messaging and broadcasting applications, such as live chat and streaming. It supports WebSocket subprotocols, such as STOMP and MQTT, which make it possible to exchange messages between clients and servers.


3. Configure Apache Reverse Proxy for Websockets

As you already know, Websocket or WS is just a persistent full duplex TCP connection with framed messages where the initial handshake is like HTTP. You need some server that listens for incoming WS requests and binds handlers to them. Nowadays, this is very easy to do with the Apache HTTP Server, with the support of the ws_tunel module, Apache can forward requests from the client computer to the Apache server computer.

This post will cover how to configure an Apache server already running on FreeBSD to proxy a websocket connection to our backend of choice. In this article we will use the "Jenkins" application as the Backend.

We suggest you read the previous article about Installing Jenkins on FreeBSD.




To start, we'll need to enable the mod_proxy module in Apache. Make sure the below modules are enabled in Apache "/usr/local/etc/apache24/httpd.conf".

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule rewrite_module modules/mod_rewrite.so

Also activate the module

LoadModule vhost_alias_module libexec/apache24/mod_vhost_alias.so

Don't forget to remove the "#" sign in the script below.

Include etc/apache24/extra/httpd-vhosts.conf
Listen 192.168.5.2:8080
ServerName www.unixwinbsd.site:8080

Next, we need to create an Apache configuration file containing the virtual hosts. On FreeBSD this file is called "httpd-vhosts.conf" which is located in the /usr/local/etc/apache24/extra directory. Rewrite the rules as below in the file /usr/local/etc/apache24/extra/httpd-vhosts.conf.

<VirtualHost 192.168.5.2:8080>
  ServerName unixwinbsd.site
  
  ProxyRequests on
  RequestHeader set X-Forwarded-Proto "http"	
  ProxyPass /	 ws://192.168.5.2:8180/
  ProxyPassReverse /	 ws://192.168.5.2:8180/  

  RewriteEngine on
  RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
  RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
  RewriteRule .* ws://192.168.5.2:8180%{REQUEST_URI} [P]
  
</VirtualHost>


This will proxy all WebSocket connections from /ws/ to ws://192.168.5.2:8180/. Finally, restart Apache to apply the changes.
root@ns6:~ # service apache24 restart
Performing sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 2103.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.
root@ns6:~ #
The next step, open the Google Chrome browser, and type in the Apache IP and port, namely: "192.168.5.2:8080". If there is nothing wrong with all the configurations above, it will appear as shown in the image below.



Because Jenkins is the Backend of Apache Websocket, Google Chrome will display the Jenkins application. Now try typing the IP and ports 192.168.5.2:8180, then the results will be the same as the display above. Because 8180 is the Jenkins port and 8080 is the Apache Websocket port.

Apache WebSocket Server is a powerful technology that is revolutionizing the way we communicate on the web. If you want to build real-time web applications, Apache WebSocket server is worth considering, as it offers faster and more efficient communications, real-time messaging, cross-platform compatibility, and scalability. Apache WebSocket server remains a popular choice among developers due to its advanced features and robust performance.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post