Search code examples
phpsocketsvagrantratchet

Ratchet WebSocket won't work over Vagrant machine


I have a WebSocket server using Ratchet (literally the example app). I'm serving it to localhost:8080 on my Vagrant machine (which is a CentOS 6) and trying to connect to it through the private network IP set in the Vagrantfile 192.168.33.10.

I'm getting a connect ECONNREFUSED 192.168.33.10:8080 (the uri is ws://192.168.33.10:8080/chat).

I already exposed the port config.vm.network "forwarded_port", guest: 8080, host: 8080.

I've tried serving the server (inside vagrant) to localhost, 127.0.0.1 and 192.168.33.10, but the client still can't connect.

It works fine outside of Vagrant using localhost on both client and server.

What am I missing?


Solution

  • If you bind the app to localhost there is no way you can access it via 192.168.33.10, hence port forwarding won't work either.

    You need to make your app to listen on 192.168.33.10:8080.

    Also add the guest's IP address to the Vagrantfile:

    config.vm.network "forwarded_port", guest_ip: "192.168.33.10", guest: 8080, host: 8080