Search code examples
laravelsocket.iobroadcastinglaravel-echo

Socket.io client can not connect to the laravel-echo-server


I have no idea why my setup does not work.

I have set up my application on vps server and im trying to get broadcasting working on but without full success.I can not make socket client to connect to the server due to timeout?it keeps trying to recconect but fail to do so.

Set up on client side:

import SocketClient from 'socket.io-client';
import Echo from "laravel-echo"
import store from './store'
import {renderResponseMessage} from 'Components/ChatWidget'
import {increaseNewMessagesCount} from 'Actions/chatActions'

window.io = SocketClient;
window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});


window.Echo.channel('chat')
    .listen('NewChatMessage', ({message}) => {

        store.dispatch(increaseNewMessagesCount());
        renderResponseMessage(message)

    })

const getSocketId = () => window.Echo.socketId();

export {
    getSocketId
}

laravel-echo-server.json

{
        "authHost": "http://localhost",
        "authEndpoint": "/broadcasting/auth",
        "clients": [],
        "database": "redis",
        "databaseConfig": {
                "redis": {},
                "sqlite": {
                        "databasePath": "/database/laravel-echo-server.sqlite"
                }
        },
        "devMode": true,
        "host": null,
        "port": "6001",
        "protocol": "http",
        "socketio": {},
        "sslCertPath": "",
        "sslKeyPath": "",
        "sslCertChainPath": "",
        "sslPassphrase": "",
        "apiOriginAllow": {
                "allowCors": false,
                "allowOrigin": "",
                "allowMethods": "",
                "allowHeaders": ""
        }

laravel-echo-server monitor The client did not connected to the server.

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

Channel: chat
Event: App\Events\NewChatMessage
CHANNEL chat

nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/LegendaJaroslaw/public/;
        index           index.php;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
           try_files $uri $uri/ /index.php?$query_string;
        }

         error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

I did use broadcasting on the vagrant homestead and it worked, but i can not find out solution for production server.Is this related to the nginx configuration? i cant tell comparing production config and from homestead.


Solution

  • I have try multiple variations of host in laravel-echo-server.json. This values did not work ['127.0.0.1','localhost', null] but "0.0.0.0" did the trick

    If anyone could explain why it worked or point to some source i would be glad!