Search code examples
node.jslaravelsocketssocket.iolaravel-echo

Laravel Echo with NodeJS HTTPS doesn't work


I've been developing this NodeJS application that takes advantage of Laravel Echo's functionalities to receive information from a server through socket connection.

Server-side Laravel Echo Server with Laravel 5.7.19

Client-side "laravel-echo": "^1.5.2" "socket.io": "^2.2.0"

import Echo from '../../node_modules/laravel-echo/dist/echo.common.js'
import Socketio from 'socket.io-client';

let echo = new Echo({ 

  broadcaster: 'socket.io',
  host: 'https://smartfish.danymota.com:8080/',
  encrypted: true,
  secure: true,
  client: Socketio,
  auth: {
    headers: {
      'Authorization': 'Bearer ' + this.token.bearerToken,
    },
  },
});
echo.private('central.' + macAddress)
  .listen('RulesUpdated', (response) => {
    handleRules(JSON.parse(response.aquarios))
    console.log(new Date().toLocaleString() + " - Rules updated")
  })

Problem Everything works fine in Http, when I switch to HTTPS it just stops working. Also, the socket connection doesn't reach the server (or at least Laravel-echo-server doesn't log it)

IMPORTANT - What I've tried

  1. Ran the application through Browserify, and then on the browser (it works just fine on a browser, even with HTTPS)

  2. Played around with different ports (Again, it works with HTTP, so ports are likely not the problem)

  3. Changed the URL to wss://, /socket.io

  4. Forced socket.io to include a secure: true on options

  5. Changed the version of Laravel Echo

  6. Tried importing both echo.common.js and echo.js

Notes

/api/broadcasting/auth - This is working, so problem is likely not here

Laravel echo server configuration

    {
    "authHost": "https://smartfish.danymota.com",
    "authEndpoint": "/api/broadcasting/auth",
    "clients": [{
        "appId": "f7506b5e7118092c",
        "key": "9015d93999f3a2f7f95a054a76fbcbfd"
    }],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath1": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "8080",
    "protocol": "https",
    "socketio": {},
    "sslCertPath": "/home/danymota/ssl/cert/smartfish.danymota.com.crt",
    "sslKeyPath": "/home/danymota/ssl/private/smartfish.danymota.com.key",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "http://smartfishweb.test/api",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

Socket.io debug

socket.io-client:url parse https://smartfish.danymota.com:8080/socket.io +0ms
socket.io-client new io instance for https://smartfish.danymota.com:8080/socket.io +0ms
socket.io-client:manager readyState closed +0ms
socket.io-client:manager opening https://smartfish.danymota.com:8080/socket.io +0ms
socket.io-client:manager connect attempt will timeout after 20000 +4ms
socket.io-client:manager readyState opening +1ms
socket.io-client:manager connect_error +60ms
socket.io-client:manager cleanup +0ms

Thank you all in advance.


Solution

  • I've solved this problem by adding a flag rejectUnauthorized: false to laravel echo.

      this.echo = new Echo({
      broadcaster: 'socket.io',
      host: config.ECHO_SERVER,
      client: Socketio,
      rejectUnauthorized: false,
      auth: {
        headers: {
          'Authorization': 'Bearer ' + this.token.bearerToken,
        },
      },
    })