dimanche 30 avril 2017

Laravel Echo Event broadcasting fails to receive on Listener

the idea of Broadcasting in Laravel is great, but it's hard to figure out how to make this to work. Even after reading the docs multiple times, watching many tutorials.. Probably I'm just missing on little thing. Who knows it?

The following code I'm running in a Laravel 5.4 project on Homestead:

# Event
class NewNumber implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public $number;

    public function __construct($number)
    {
        $this->number = $number;
    }

    public function broadcastOn()
    {
        return new Channel('pub-channel');
    }
}

# EventServiceProvider:
protected $listen = [
        'App\Events\NewNumber' => [
            'App\Listeners\DoSomeThingsWithNewNumber',
        ],
    ];

The config of broadcasting is set to Redis. I installed Laravel Echo Server and run this with laravel-echo-server start and see things in the console like [11:15:16 AM] - v7y-5DsMXHdBXcqzAAAA joined channel: pub-bingo

The following config it created:

{
    "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": ""
}

In the javascript, I added:

import Echo from "laravel-echo";

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

console.info(window.Echo.channel('pub-channel'));

window.Echo.channel('pub-channel')
    .listen('NewNumber', (e) => {
        console.info(e);
        alert('Something happened');
        console.log(e.number);
    });

The console in Chrome shows the .info with the information about the channel. All looks fine as far as I can see.

But what doesn't happen, i the alert nor the console.loggin in the listen method.

I have an URL that I trigger in different browser that has this inside: broadcast(new \App\Events\NewNumber(rand(1, 100));

And something the application logs something in the Laravel-logs that it's successful done when I trigger that url:

[2017-04-30 11:38:44] local.INFO: Broadcasting [App\Events\NewNumber] on channels [pub-channel] with payload:
{
    "number": 54,
    "socket": null
}  

And I have the php artisan queue:listen running also (QUEUE_DRIVER: database). But database stays empty..

I really can't figure out anymore what I'm missing.. What is missing?!...



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2qlgSDD
via IFTTT

Aucun commentaire:

Enregistrer un commentaire