tlaverdure / laravel-echo-server

Socket.io server for Laravel Echo
MIT License
2.65k stars 511 forks source link

Client side not picking up on any event its listening to #458

Open pknad505 opened 4 years ago

pknad505 commented 4 years ago

I'm attempting to use laravel-echo-server however I'm getting a roadblock where the client-side doesn't appear to be getting any broadcasted events.

Everything seems to be connected fine and socket.io server is reporting that it's connecting and authorising the user however when I broadcast a message nothing seems to happen. The event is appearing on Laravel Horizon but otherwise, nothing happens. Here is my code:

server.js to run the laravel-echo-server:

require('dotenv').config();

const env = process.env;

require('laravel-echo-server').run({
    authHost: env.APP_URL,
    devMode: env.APP_DEBUG,
    database: "redis",
    databaseConfig: {
        redis: {
            host: env.REDIS_HOST_PUBLIC,
            port: env.REDIS_PORT,
        }
    }
});

My channel in channel.php

Broadcast::channel('message.pushed', function () {
    return true;
});

My event:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MessagePushed implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->message = "My New Message";
    }

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

My event listener within app.js

import Echo from 'laravel-echo';
window.io = require('socket.io-client');

if (typeof io !== 'undefined') {
    var token = $('meta[name="_token"]').attr('content')
    window.Echo = new Echo({
        auth       : {
            headers: {
                Authorization: `Bearer ${token}`,
            },
        },
        broadcaster: 'socket.io',
        host       : window.location.hostname + ':6001',
    });
}

function listenForBroadcast() {
    Echo.private('message.pushed')
    .listen('MessagePushed', (e) => {
        console.log(e)
        console.log("Listened")
    });
}

listenForBroadcast();

Lastly the route sending the message:

Route::get('/event-test', function () {
    broadcast(new App\Events\MessagePushed());
});

It does out put in the laravel-echo-server console this:

Channel: laravel_database_private-message.pushed
Event: App\Events\MessagePushed
YordaniBonilla commented 4 years ago

https://stackoverflow.com/questions/57360229/cant-remove-laravel-database-prefix-from-channel

Try removing the prefix worth a shot. Also after I clear pretty much every cache the app picks up the change of channel name when start up the server. https://tecadmin.net/clear-cache-laravel-5/