0

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>

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

the problem is with your javascript code,just try to send the msg in websocket.onopen listener that's how you assure your WebSocket'isn't in CONNECTING state. below is an example :

window.addEventListener("DOMContentLoaded", function (event) {
  const socket = new WebSocket("ws://localhost:8745/");
  socket.onopen = function () {
  socket.send("hey there)
  };
  socket.onmessage=function(event){
    console.log(event)
  }
  socket.onerror = function(error) {
    console.error("WebSocket error:", error);
  };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.