Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel echo with redis and socket io

i have an event SomeEvent.php

like so:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class SomeEvent  implements ShouldBroadcast
{


    use InteractsWithSockets, SerializesModels;

   public $data;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($array)
    {
        $this->data = $array;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

i have included the following in my bootstrap.js and compiled it with gulp

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'http://site.dev:6001'

});

window.Echo.private('channel-name')
    .listen('SomeEvent', (e) => {
        console.log(e);
});

then i have installed tlaverdure/laravel-echo-server and here is my laravel-echo-server.json

{
    "appKey": "[generated]",
    "authHost": "http://site.dev",
    "authEndpoint": "/broadcasting/auth",
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": false,
    "host": "sitei.dev",
    "port": "6001",
    "referrers": [],
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

now when i fire up laravel echo server with laravel-echo-server start it starts up very well however when i fire up the above event like so

event(new SomeEvent(json_encode(['name' => 'some-name'])));

i can see the event published to redis however nothing is loged to my client console: i am also including socket io in my master.blade.php

the above also happens with notifications

any help will be highly appreciated. Thanks guys

like image 296
user2293554 Avatar asked Nov 02 '16 16:11

user2293554


1 Answers

Have you defined an authentication rule for your channel in BroadcastServiceProvider?

Do you see any information about connecting and leaving the channel if you put on devMode in the laravel-echo-server.json?

I had a lot of troubles trying to set up my websocket connection, but a finally figured it out and my code looks pretty much like yours.

Good luck!

like image 105
gambrosino Avatar answered Nov 15 '22 21:11

gambrosino