Search code examples
phpwebsocketvagrantratchet

Websockets and Vagrant


I am using websockets in PHP with this Ratchet library. It is working great in my local machine(without vagrant).

Server-side code:

$loop = React\EventLoop\Factory::create();
$realtimeApp = new Ratchet\App('localhost', 2282, '0.0.0.0', $loop);
$loop->run();

Client-side code:

var conn = new WebSocket('ws://localhost:2282');
conn.onopen = function (e) {
    console.log("Connection established!");
};
conn.onerror = function (e) {
    console.log("Connection error!");
};
conn.onclose = function (e) {
    console.log("Connection closed!");
    console.log(e);
};

But, when I use it in my virtual machine(with vagrant), it does not work.

Server-side code:

$loop = React\EventLoop\Factory::create();
$realtimeApp = new Ratchet\App('192.168.56.101', 2282, '0.0.0.0', $loop);
$loop->run();

Client-side code:

var conn = new WebSocket('ws://192.168.56.101:2282');
conn.onopen = function (e) {
   console.log("Connection established!");
};
conn.onerror = function (e) {
    console.log("Connection error!");
};
conn.onclose = function (e) {
    console.log("Connection closed!");
    console.log(e);
};

Solution

  • I used puphpet to generate puppet manifests. The problem was caused by a firewall. By default, puphpet blocks all the ports except 22(for SSH) and 80(for apache and ngnix). I created firewall rule from the puphpet GUI and run vagrant provision and it worked.