How to use Laravel Echo on production server with https

15,261

Solution 1

This is how I solved the problem

First. You have to configure your nginx vhost with ssl (no websocket connected yet). I use Let's encrypt ( https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04)

Second. You could create a path on your vhost to proxy to your websocket. This way nginx handles ssl protocol and also you do not use another port

location /ws/{

    proxy_pass http://127.0.0.1:3000/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $remote_addr;
}

After that your script will have to connect to `https://localhost/ws/

Note: I use port 3000 for my laravel-echo-server intance and not 6001 as stated in the original question

Solution 2

In addition to the user237329 response, the laravel echo setting in javascript should be:

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.host,
    path: '/ws/socket.io',
});
Share:
15,261
MMMTroy
Author by

MMMTroy

I'm a software developer with over 10 years of PHP, HTML, CSS & Javascript experience. Recently I've been playing with C#, .NET Core & Flutter. Currently I'm a Software Developer for Jobutrax Field Ticketing Software at Pandell Technology.

Updated on June 14, 2022

Comments

  • MMMTroy
    MMMTroy almost 2 years

    I'm wondering what's the proper (or any for that matter) way of setting up Laravel Echo on an https production server. I've had it working on my local vagrant for a little bit now, and now that I've pushed the changes to production I'm unable to get the script to connect to the node server. Here is what I currently have.

    var echo = require('laravel-echo-server');
    
    var options = {
        host: 'https://localhost',
        port: '6001',
        sslCertPath: '/etc/nginx/ssl/nginx.crt',
        sslKeyPath: '/etc/nginx/ssl/nginx.key'
    };
    
    echo.run(options);
    

    And then in javascript

    import Echo from "laravel-echo"
    window.echo = new Echo({
        broadcaster: 'socket.io',
        host: 'https://localhost:6001'
    });
    

    The above configuration is how I started out, but I've tried many other combinations, including trying to edit the nginx configuration to bypass https all together. If bypassing https is the method that's required, any advice on how to do this with Laravel Echo would be appreciated since the socket.io threads on this topic that I've been referencing don't seem to do the trick for me.