Search code examples
phplinuxwebsocketapache2ratchet

PHP Ratchet Websocket Server With Apache2 Not Working


I have a websocket server written in php, it's the Ratchet websocket server. I enabled wstunnel for apache. I also added ProxyPass in my website configuration

This is my apache configuration for my website.

 <VirtualHost *:443>
 DocumentRoot /var/www/mywebsite/pav/
 ServerName www.mywebsite.com
 ServerAlias mywebsite.com

 RewriteEngine on
 ProxyPass /wss2/ ws://www.mywebsite.com:8080

 
 <Directory /var/www/mywebsite/pav/>
 DirectoryIndex index.php
 AllowOverride All
 Order allow,deny
 Allow from all
 </Directory>
 </VirtualHost>

My website is written php and JS. In javascript the script I used to connect to the server is

 var socket = new WebSocket('wss://www.mywebsite.com:8080');

I checked in my browser (Firefox) using inspect and in the console it says: Firefox can’t establish a connection to the server at wss://www.xpatvoice.com:8080/.

I've tried other browsers and got the same result.

The thing is I don't know how to tell what's causing it to fail, I mean any error code or error log to diagnose the problem.

I also tried


 var socket = new WebSocket('wss://www.mywebsite.com/wss2/:8080');

Here's the chat socket class

namespace MyApp;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;


class Socket implements MessageComponentInterface {

    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
        $this->users = [];
        $this->inqs = [];
    }

    public function onOpen(ConnectionInterface $conn) {

        // Store the new connection in $this->clients
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
        //echo $conn->httpRequest->headers["X-Forwarded-For"][0];
                
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        
        $data = json_decode($msg);
        $userinfo = json_decode($data->userinfo,true);

       if(isset($userinfo["userId"]) && $data->action == "userLogin"){
                $userID = $userinfo["userId"];
                $this->users[$userID]["conn"] = $from;
                $this->users[$userID]["role"] = $userinfo["role"]; 
                echo "attached user $userID\n";
           
           // NOTIFY INQUISITORS OF ADMIN LOGIN
                if($userinfo["role"] < 3){
                    foreach($this->inqs as $inq => $client){
                        $message["action"] = "adminLogon";
                        $client->send(json_encode($message));
                    }
                 } 
       }
        
        if(isset($userinfo["userId"]) && $data->action == "adminReply"){
            
            $this->inqs[$data->to]->send($msg);
            
        }
        
           if(isset($userinfo["tempuser"]) && $data->action == "enquiry"){
                $userID = $userinfo["tempuser"];
                $this->inqs[$userID] = $from;
                echo "attached inquisitor $userID\n";
          }
        
        if(isset($userinfo["tempuser"])){
            
            foreach($this->users as $userid => $user){
                if($user["role"] < 3){
                    $user["conn"]->send($msg);
                }
            }
            echo "message from inquisitor\n";
         }
        

    }

    public function onClose(ConnectionInterface $conn) {

      $this->clients->detach($conn);

    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}

I enabled proxy, proxy_wstunnel, proxy_http with


        sudo a2enmod proxy
        
        output: Module proxy already enabled
        
        sudo a2enmod proxy_wstunnel
        
        output: Module proxy_wstunnel already enabled
        
        sudo a2enmod proxy_http
        
        output: Module proxy_http already enabled

I'm using Apache/2.4.41


Solution

  • So I followed hakre's advice and looked at the php/apache error log located in /var/log/apache2/error.log and this error came up whenever I tried to connect to the socket server from a browser.

    AH00898: DNS lookup failure for: www.mywebsite.com:8080 returned by /wss2/:8080

    I googled it and found my answer in this link here

    https://unix.stackexchange.com/questions/240590/why-is-apache-giving-dns-lookup-failures

    As it turns out the error was in the apache configuration for my website.