0
votes

I've been trying to get websockets in nginx/php working for days now.

Ths is my current setup :

'Chat.php' path = root/src/MyApp/Chat.php

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
    protected $clients;
    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }
    public function onOpen(ConnectionInterface $conn) {
    // Store the new connection to send messages to later
         $this->clients->attach($conn);
         echo "New connection! ({$conn->resourceId})\n";
    }
    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
        , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
        foreach ($this->clients as $client) {
             if ($from !== $client) {
            // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }
    public function onClose(ConnectionInterface $conn) {
    // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);
        echo "Connection {$conn->resourceId} has disconnected\n";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

'chat-server.php' path = root/bin/chat-server.php

<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
    require dirname(__DIR__) . '/vendor/autoload.php';
    $server = IoServer::factory(
        new HttpServer(
            new WsServer(
                new Chat()
            )
        ),
        8080
    );
    $server->run();

Nginx conf

 location / {
            access_log off;
            proxy_pass http://localhost:8080;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_read_timeout 86400s;
            proxy_send_timeout 86400s;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }

Using the Ratchet examples. I can get the normal CLI only messaging working just fine. If I try it in the browser, this error pops up in the console:

WebSocket connection to 'ws://localhost:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

The server was indeed running at that point. Any ideas?

1
I think, Ratched can working without nginx, and I try to debug without nginx, try to check if ports are opened (telnet localhost 8080). Is it worked without nginx (try to connect in js directly to 127.0.0.1 and 8080)? - Pavel Shorokhov
You get error "WebSocket connection to 'ws://localhost:8080/' failed" in browser, so it means that client connects directly to ratchet (port 8080). Is Is it so? Show your js code for connection - Max P.

1 Answers

0
votes

you have to access your websocket from the browser using:

new WebSocket('ws://localhost/');

Nginx will then proxy this request (any request on /) to http://localhost:8080; where your PHP websocket implentation is listening on... hopefully