Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcasting with Laravel Echo, laravel-echo-server and socket.io won't work

I've setup websockets with Laravel sucessfully with an own implementation. Now I'd like to switch to Laravel Echo and laravel-echo-server. But, after many hours of trying and reading every piece of documentation I could find, I do need further help.

What's happening when I fire an event: The queue worker processes the event as desired: "Processed: Illuminate\Broadcasting\BroadcastEvent". But nothing happens in the laravel-echo-server console nor on the client.

Some information:

  • Laravel is running on port 8001

  • Redis is running on 6379

  • the queue worker is running (php artisan queue:work)

  • Laravel, Redis and laravel-echo-server are running on the same machine (192.168.134.214)

  • when trying private channels the authentication in the BroadcastServiceProvider seems to work (the server writes errors to the console if it fails)

  • the client uses the socket.io script of the laravel-echo-server: <script src="//192.168.134.214:6001/socket.io/socket.io.js"></script>

Excerpt of my .env:

BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

The laravel-echo-server.json:

{
    "appKey": "<myAppKey>",
    "authEndpoint": "/broadcasting/auth",
    "authHost": "http://localhost:8001",
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "http://localhost"
        }
    },
    "devMode": true,
    "host": "192.168.134.214",
    "port": "6001",
    "sslCertPath": "",
    "sslKeyPath": ""
}

app.js

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://192.168.134.214:6001',
});

app.blade.php

<script>
    Echo.channel('mychan')
        .listen('myevent', (e) => {
            console.log('Hello World!', e);
        });
</script>

Parts of the Event "NewsPublished":

class NewsPublished implements ShouldBroadcast {
    use InteractsWithSockets, SerializesModels;

    public function broadcastOn() {
        return new Channel('mychan');
    }

    public function broadcastAs() {
        return 'myevent';
    }
}

..and I'm firing the Event with event(new App\Events\NewsPublished());

I was hoping to get some information out of the laravel-echo-server when switching "devMode" to true. But that doesn't seem to change anything!

like image 349
halloei Avatar asked Feb 05 '23 22:02

halloei


1 Answers

In your redis configuration, you've included http:// as a protocol with the host. You will need to remove the protocol and simply use localhost or 127.0.0.1

{
...
"databaseConfig": {
    "redis": {
        "port": "6379",
        "host": "localhost"
    }
...
}

*Aside: If you have the ability to use a socket, consider using "path": "/path/to/sockfile" in-place of "host"; that's moreso about performance but I'm sure this correction should get it working.

For reference, here's a redacted version of the laravel-echo-server.json configuration that I am using.

{
  "appKey": [omitted],
  "authEndpoint": "/broadcasting/auth",
  "authHost": "http://localhost",
  "database": "redis",
  "databaseConfig": {
    "redis": {
      "db": 2, /*this is an intentional change; the default is zero(0)*/
      "path": "/tmp/redis.sock",
      "password": [omitted]
    }
  },
  "devMode": false,
  "host": "",
  "port": "6001",
  "referrers": [
    {
      "host": "*", /*matches any referrer*/
      "apiKey": [omitted]
    }
  ],
  "sslCertPath": "",
  "sslKeyPath": "",
  "verifyAuthPath": true,
  "verifyAuthServer": false
}
like image 119
Richard Brown Avatar answered Feb 08 '23 15:02

Richard Brown