Ad

Use Laravel Echo With Docker (CORS Problem)

- 1 answer

I want to use Laravel Echo in the following way:

I have two docker containers, one for laravel (php) and one for the socket server (https://hub.docker.com/r/mintopia/laravel-echo-server).

Now I have the problem, that Laravel Echo can't connect to the server, because of CORS.

I already found one option for the echo server, so I added ECHO_ALLOW_ORIGIN=http://php:80 to the enviromnent variables. Unfortunately this changes nothing.

Can someone pls tell me how to fix this?

Ad

Answer

I use k1sliy/laravel-echo-server, but the locations/commands should be similar.

You share a directory with your TSL/SSL cert and laravel-echo-server.json or just the files themselves. For example, I start mine with something like (note I think my port is non-standard for echo because I need one cloudflare will proxy):

docker run -d --name echo \
-p 8443:8443 \
-v YOURPATH/laravel-echo-server.json:/app/laravel-echo-server.json \
-v YOURPATH/privkey.pem:/app/privkey.pem \
-v YOURPATH/cert.pem:/app/cert.pem k1sliy/laravel-echo-server

You'll want to edit the laravel-echo-server.json file and make sure it has this in it (where YOUR_ORIGIN_HERE is the orgin you want to allow) and destroy and recreate the docker container to force it to reread the config:

  "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": " YOUR_ORIGIN_HERE ",
        "allowMethods": "OPTIONS, GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }

The origin is the origin as the host/client browser sees it. php is likely the hostname in the docker containers mapped to the private 172 network -- which isn't likely to be what you want. You want that to be whatever you are typing into the address bar (without protocol) of the browser to access the site, likely 128.0.0.1, localhost or 192.168.X.X followed by a colon and the port (likely 80 or 443 ... you can also do a * for port to allow any port to talk to the echo server).

Ad
source: stackoverflow.com
Ad