G
G
grigorie19902020-01-31 10:56:19
Push technologies
grigorie1990, 2020-01-31 10:56:19

How to see event message in pusher socket channel (Laravel Websockets)?

I have an event that should broadcast to a channel, but when it is called, no message is visible in the channel. I am using Laravel Websockets. On the server, the port is opened and I receive the channel object on the client

import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    wsHost: window.location.hostname,
    wsPort: 6001,
});
let channel = window.Echo.channel('room1');
console.log(channel);
channel
    .listen('TestEvent', () => {
        console.log('1');
    });

event class
class TestEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $data;
    public function __construct()
    {
        $this->data = json_encode(['a' => 1, 'b' => 2]);
    }

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

console log(channel); - fires
console.log(1) after calling the route
Route::get('test', function() {
    App\Events\TestEvent::dispatch();
});

does not work
Do you have any thoughts?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NoBody1, 2020-02-17
@NoBody1

Event must use ShouldBroadcast if WebSockets are used. More details are written in the laravel docs. But in short, you need something like this:
Class TestEvent implements ShouldBroadcast {
...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question