Nuxt JS Laravel echo PrivateChannel not receiving any data

Published

I’m using Laravel 8, as an API, and a Nuxt JS front-end using the tymon auth package and v5 of the auth module. I’ve set up a private channel, and am trying to receive data but nothing is coming through, public channels work fine, I’m using the pusher replacement option and the websockets integration.

What am I missing?

appEventsSendFetchBeamSignal.php

<?php

namespace AppEvents;

use AppModelsUser;
use IlluminateSupportFacadesLog;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;

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

    /**
     * The user
     */
    protected $user;

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

    /**
     * Get the data to broadcast.
     *
     * @return array
     */
    public function broadcastWith()
    {
        return [
            'hello' => 'hello'
        ];
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return IlluminateBroadcastingChannel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('user.'.$this->user->id);
    }
}

routes/channels.php

Broadcast::channel('user.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

My JS:

this.$echo.private('user.1').listen('SendFetchBeamSignal', (data) => {
  console.log('received')
  console.log(data)
})

And for now, I’m broadcasting a notification via a route…

Route::get('/broadcast', function () {
    broadcast(new SendFetchBeamSignal(User::find(1)));
});

Source: Laravel

Published
Categorised as laravel, php, pusher, socket.io, websocket Tagged , , , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq