Websockets for Clojure Sente in Apache Reverse Proxy

Table of Contents

img

The Clojure library sente allows you to do smooth web-socket work in Clojure. In our case, we wanted the typical “Your Session Expires in N seconds” alert for users of the app. Sente was working smoothly on our local machines, but we were getting obscure errors in the console about 500 responses in our browser console.

The Problem

As mentioned, our console was showing server errors on the path that was supposed to be doing the Sente channels. Our setup on this app is that we are running a full Clojure[script] stack producing an uberwar that uses Immutant, to be deployed on a Wildfly server, where Wildfly is reverse-proxied by Apache. In other words, the first issue is to figure out where in this process the error is occurring. Eventually we ruled-out that the problem was in either the WAR, Immutant, or Wildfly, meaning that the issue was with Apache.

The Solution

It turns out Web Sockets have a protocol all their own and that Apache needs to be tweaked to handle them – while our Apache conf was already forwarding HTTP to HTTPS (ModRewrite), and also already sending regular traffic to Wildfly (Reverse Proxy), it was not handling WS:// requests, which required a tiny bit of elbow grease.

First, enable the right modules in Apache

These didn’t require any downloads but were already availdble in my Apache from the Ubuntu repo. In the shell we ran:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel

Then, set up your conf to reroute socket requests

Note that the original version used localhost instead of 127.0.0.1, but localhost fails on my OpenSUSE box while 127.0.0.1 works everywhere.

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*)           ws://127.0.0.1:8080/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*)           http://127.0.0.1:8080/$1 [P,L]
# END websockets

Resources

Tory Anderson avatar
Tory Anderson
Full-time Web App Engineer, Digital Humanist, Researcher, Computer Psychologist