dib258's avatar
Level 11

Broadcast PresenceChannel between two people

Hi there,

I'm trying to make Broadcasted event work for a case where two user share a conversation (like a chat) where only the two of the can access and get notified when a new message comes.

I think, after reading the documentation that the presence channel is the best option (private is for one people and the server and channel is for something plublic without checking ?)

So in my routes/channels.php I have something like this :

Broadcast::channel('conversation.{conversation}', function ($user, Conversation $conversation) {
    if ($user->id === $conversation->user1_id || $user->id === $conversation->user2_id) {
        return $user;
    }
    
    return null;
});

In the client side a Component I have :

    Echo
    .join('conversation.${conversation.id}')
    .here((users) => {
        this.usersInRoom = users;
    })
    .joining((user) => {
        this.usersInRoom.push(user);
    })
    .leaving((user) => {
        this.usersInRoom = this.usersInRoom.filter(u => u != user);
    })
    .listen('MessagePosted', (e) => {
        this.messages.push({
            id :e.message.id,
            user_id :e.message.user_id,
            conversation_id :e.message.conversation_id,
            user :e.user,
            text :e.message.text,
            created_at :e.message.created_at,
            updated_at :e.message.updated_at,
        });
    });

And the class that emit the even MessagePosted :

    public function broadcastOn()
    {
        return new PresenceChannel('conversation.'.$this->message->conversation_id);
    }

So I now that previously I used a PresenceChannel without any checking, so if two people where in there conversation they would get notification from everyone. not the right thing.

In the server side, I have the 'conversation.{conversation}' that was mentioned in the documentation to make a separated channel. But I also saw something like 'conversation.*'.

And on the client side I have join('conversation.${conversation.id}') but here I am not sure at all I just know that I have in the props (props : ['user', 'friend', 'conversation']) a conversation which is an object with the id of the conversation.

So When, when everyone was on the same channel with no restriction everything was working perfectly and now, I think I have an error which make the whole thing not work.

I have two 500 server side error when I load a client conversation :

ReflectionException in Broadcaster.php line 170:
Class Conversation does not exist

(And in the route/channels.php I import the Conversation class use App\Conversation;)

and

HttpException in Broadcaster.php line 154:

Thanks in advance if you can help me configure all of this !

0 likes
3 replies
dib258's avatar
Level 11

No one is using the broadcasted events ? :p

dib258's avatar
Level 11

@JeffreyWay In the documentation for Laravel Echo, It's not clear of the way to join a room. Still in the documentation, chat.${roomId}, what is roomId ? A simple variable that could be in the data or props ? In my case, the id of the conversation is in a prop, then can I do conversation.${conversation.id} ?

@bobbybouwmann (It seems you always have the right solution ! :p)

bobbybouwmann's avatar

I haven't worked much with Laravel Echo yet so I can't help you right away. It's on my list to dive into it.

Did you already solve your problem? If you are still stuck I would suggest to first get it to work with a public channel and then later on switch it to a different channel type.

Please or to participate in this conversation.