Anzyme
Anzyme

Reputation: 3

Can't receive events broadcasted on Laravel

I've been struggling for a while now and can't find a way to make this work. My project is on Laravel 8 I'm using Redis and laravel-echo-server (since it doesn't seem maintained I also tried with a hand-made express/socket.io server)

The problem : When I fire the event, it's received by Redis and Laravel Echo Server but it's never received on my webpage's console.

"Events/SendMessage"

<?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\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

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

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

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

    public function broadcastWith()
    {
        return [
            'title' => 'This notification is awesome'
        ];
    }
}

".env"

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

LARAVEL_ECHO_PORT=6001

"welcome.blade.php"

<script>
        window.laravel_echo_port='{{env("LARAVEL_ECHO_PORT")}}';
    </script>
    <script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha384-cPwlPLvBTa3sKAgddT6krw0cJat7egBga3DJepJyrLl4Q9/5WLra3rrnMcyTyOnh" crossorigin="anonymous"></script>
    <script src="{{ ('/js/laravel-echo-setup.js') }}" type="text/javascript"></script>
      
    <script>
    window.Echo.channel('user-channel').listen('.SendMessage', (e) => {
        console.log('Got event...');
        console.log(e);
    });
    </script>

"laravel-echo-setup.js"

import Echo from 'laravel-echo';

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

Hope someone can help me

If you need anymore code, I'm here !!

Upvotes: 0

Views: 443

Answers (0)

Related Questions