Search code examples
websocketubuntu-16.04ratchetphpwebsocket

Ratchet PHP Websocket persistent Ubuntu 16.04


My websocket file:

<?php
require  __DIR__ . '/../../../vendor/autoload.php';

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

require 'chat.php';

// Run the server application through the WebSocket protocol on port 8080
$app = new Ratchet\App("localhost", 8080, '0.0.0.0', $loop);
$app->route('/chat', new Chat, array('*'));

$app->run();

I followed this guide for set-up my websocket: https://blog.samuel.ninja/the-tutorial-for-php-websockets-that-i-wish-had-existed/

Locally it works using php socket.php (tested with xampp on windows 10). When I try to deploy on my virtual machine (ubuntu 16.04) I have this error:

WebSocket connection to 'ws://localhost:8080/chat?id=NDY=&group=43,60' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

PS: I have try to start websocket using php /var/www/path-to-websocket/socket.php

I also need to start it with a service instead of command line, but the method described not works


Solution

  • Try to use SERVERNAME instead localhost.

    From PHP RATCHET:

    Run your website and WebSocket server on the same machine using port 8080 for WebSockets and take the chance client proxies won't block traffic
    Run your WebSocket server on its own server on port 80 under a subdomain (sock.example.com)
    Put a reverse proxy (HAProxy or Varnish) in front of your webserver and WebSocket server
    

    source: http://socketo.me/docs/deploy