WebSockets and Apache

Warning: This post is really boring. It’s about a boring problem with a boring solution, neither of these are things I actually understand. But this was a tough one and therefore should be documented.

To demo ExploAIS, my AIS decoding library, I built this website that actually shows the ships on a map. The server, running on Paisley, listens to the Exploratorium’s AIS feed, decodes the data using ExploAIS, and updates the client using Socket.IO. The problem is that Paisley runs many applications and websites (visited by absolutely nobody), and so once embedded into the Apache framework, the ShipMapper client could not connect to the server’s Socket.IO instance.  It took me some time to figure out I had to install mod_proxy_wstunnel on Apache. As the name suggests, it enables tunneling of web socket connections to the backend server.

I futzed around with many Apache settings, but got all kinds of errors. What ended up solving it was using a virtual host. I got a brand new domain from noip.com (hey, I get like 30 of these, and I only use a handful!) and played with the settings some more. At the end, apache2.conf had this segment:

<VirtualHost *:80>
  ServerName http://shipmapper.no-ip.net
  RewriteEngine On

  RewriteCond %{QUERY_STRING} transport=polling
  RewriteRule /(.*)$ http://localhost:1337/$1 [P]

  ProxyRequests off
  ProxyPass /socket.io/ ws://localhost:1337/socket.io/ retry=0
  ProxyPassReverse /socket.io/ ws://localhost:1337/socket.io/ retry=0

  ProxyPass / http://localhost:1337/
  ProxyPassReverse / http://localhost:1337/
</VirtualHost>

Please don’t ask me what this all means. I really don’t know. In the html file, the javascript code includes this:

var socket = io('ws://shipmapper.no-ip.net/');

Here’s an interesting bit: when sitting down to write the post, I tried to reproduce the errors that I got (I really need to better documenting these). I commented out a bunch of lines in the apache2.conf file, and it still worked. So effectively, my file looks like this:

<VirtualHost *:80>
  ServerName http://shipmapper.no-ip.net
  ProxyPass / http://localhost:1337/
  ProxyPassReverse / http://localhost:1337/
</VirtualHost>

So… yeah! Whatever….

Leave a Reply

Your email address will not be published. Required fields are marked *