How to add a variable to Laravel Echo.join of a presence channel to be available to its ‘here’ method?

  channels, echo, laravel, php

My video app uses Laravel’s Echo in its Vue.js frontend to listen to a presence channel to get online users through its .here method which returns user model of online users, and is working perfectly well. What I am trying to do now is to add another variable from a different model ($session->id) to be available to the ‘here’ as well.

So I created this channel class with the Session model bound to it alongside User model:

namespace AppBroadcasting;

use AppUser;
use AppSession;

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

    /**
     * Authenticate the user's access to the channel.
     *
     * @param  AppUser  $user
     * @return array|bool
     */
    public function join(User $user, Session $session)
    {

        return [ 'id' => $user->id, 'name' => $user->name, 'isPatient' => $user->isPatient(), 'isDoctor' => $user->isDoctor(), 'isCoordinator' => $user->isCoordinator(), 'sessionId' => $session->id ];
        
    }
}

Now I was trying to figure out where to feed that model a specific $session. First I thought I would do this in the backend as such:

$session = Session::find($id);
event(new userJoining($session));

The problem is: this is a presence channel that is used only in the frontend, meaning that I never boradcast an event in the backend in the first place. The presence channel is only used in the frontend to show online users.

So I thought I could do this in Laravel’s Echo in the frontend by adding a second parameter after the channel name:

Echo.join('lobby' + this.clinic.id, this.session)

But this threw a 500 error.

How can I achieve what I am trying to do?

Source: Laravel

Leave a Reply