How do I setup websockets on apache using this tutorial, with just built in apache modules?

741 views Asked by At

I'm trying to use these instructions to setup a websockets connection on my localhost apache webserver and it's not working.

I have the mod_proxy_wstunnel and mod_proxy modules turned on so I should have the capability of running websockets on localhost. Nowadays Apache webserver has websockets support built in. Gone of the days of installing third party websockets apache modules like Rachet.

I get this error after following the instructions in the above question.

Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

WebSocket connection to 'ws://socket.localhost/' failed:

I'm using apache webserver, not litespeed, lighttpd, nginx or IIS.

By the way I'm using Uniform Server.

My first attempt (in case someone comments for me to show my work)

Make sure that the following apache modules are turned on

  • mod_proxy_wstunnel
  • mod_proxy_modules

Edit the already existing file called httpd.conf to then insert this into it. Make sure you edit the correct file if you have duplicate file names for different apache versions

<VirtualHost *:80>
    ServerName socket.localhost

    ProxyRequests Off
    ProxyPass "/ws2/"  "ws://localhost:8546/"
    ProxyPass "/wss2/"  "wss://localhost:8546/"
</VirtualHost>

Test the websocket using this HTML page with inline javascript

<script type="text/javascript">
    var socket = new WebSocket('ws://socket.localhost');
    socket.send('Test');
</script>
1

There are 1 answers

0
Algo7 On

Below is an example that I use for GraphQL subscription via websocket.

IP & Port are meant to be replaced by your configuration.

Make sure you have these modules installed:

 proxy_module (shared)
 proxy_http_module (shared)
 proxy_wstunnel_module (shared)
 rewrite_module (shared)

Site Config:

<VirtualHost *:443>
        ServerName something.com
        ServerAdmin web@localhost
        DocumentRoot /var/www/html
        RewriteEngine On
        RewriteCond %{REQUEST_URI}  ^/subscription            [NC]
        RewriteCond %{QUERY_STRING} transport=websocket    [NC]
        RewriteRule /(.*)           ws://ip:port/$1 [P,L]
        ProxyPass "/subscriptions" "ws://ip:port/subscriptions"
        ProxyPassReverse "/subscriptions" "ws://ip:port/subscriptions"
        ProxyPass "/" "http://ip:port/"
        ProxyPassReverse "/" "http://ip:port/"
</VirtualHost>
/Subscription

Is my websocket endpoint. Yours might differ.