Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

joining and leaving events not triggered with Laravel Echo (PresenceChannel) #877

@kristerskz

Description

@kristerskz

I'm building a chat application with Presence Channel. The problem is that .here and .listen() methods work fine, but .joining and .leaving are never triggered.

Some googling suggested that this would happen if I returned boolean in the authorization callback in routes/channel.php. But this is not the case here. I'm definitely returning an array as per the docs.

I'm testing with one normal Chrome window and another incognito window.
Is there any other reason why .joining() and .leaving() callbacks wouldn't work? How would I debug this?

There is one suspicious thing in the output of php artisan websockets:serve (see below).
It says "channel_data":"{"user_id":1,"user_info":{"id":88647}}" for the first user and "channel_data":"{"user_id":1,"user_info":{"id":40689}}" for the second user. Notice user_id is 1 for both users. This happens even if I'm testing with one normal and one incognito browser window. Any chance this has something to do with my problem?

This is my javascript:

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: pusherAppKey,
    wsHost: wsHost,
    wsPort: 6001,
    forceTLS: true,
    disableStats: true,
});

window.Echo.join('chat')
    .here((users) => { // This works fine
        console.log('Here! Users: ', users);
    })
    .joining((user) => { // This is never triggered!
        console.log('joining ', user);
    })
    .leaving((user) => { // This is never triggered!
        console.log('leaving ', user);
    })
    .listen('NewMessage', (e) => { // This works fine
        console.log('NewMessage', e);
    });

This is what I have in routes/channels.php

Broadcast::channel('chat', function (User $user) {
    return [
        'id' => $user->id,
    ];
});

This is the output of php artisan websockets:serve if I join my chat application with two different users:

root@3503797121c0:/app# php artisan websockets:serve
Starting the WebSocket server on port 6001...
New connection opened for app key 7vMDbBNYTdsVmH.
Connection id 545942196.331978065 sending message {"event":"pusher:connection_established","data":"{\"socket_id\":\"545942196.331978065\",\"activity_timeout\":30}"}
BgfCZw9eot4J4d: connection id 545942196.331978065 received message: {"event":"pusher:subscribe","data":{"auth":"7vMDbBNYTdsVmH:182c2c2c2c4d59394d03cb8536a7334d4d3127725a88e035d8ff019660610b0f","channel_data":"{\"user_id\":1,\"user_info\":{\"id\":88647}}","channel":"presence-chat"}}.
Connection id 545942196.331978065 sending message {"event":"pusher_internal:subscription_succeeded","channel":"presence-chat","data":"{\"presence\":{\"ids\":[1],\"hash\":{\"1\":{\"id\":88647}},\"count\":1}}"}
New connection opened for app key 7vMDbBNYTdsVmH.
Connection id 239518789.527591489 sending message {"event":"pusher:connection_established","data":"{\"socket_id\":\"239518789.527591489\",\"activity_timeout\":30}"}
BgfCZw9eot4J4d: connection id 239518789.527591489 received message: {"event":"pusher:subscribe","data":{"auth":"7vMDbBNYTdsVmH:77a3caa70361058c35da74980e71d9242a041f12bee78c21ebbbfd6b778e8500","channel_data":"{\"user_id\":1,\"user_info\":{\"id\":40689}}","channel":"presence-chat"}}.
Connection id 239518789.527591489 sending message {"event":"pusher_internal:subscription_succeeded","channel":"presence-chat","data":"{\"presence\":{\"ids\":[1],\"hash\":{\"1\":{\"id\":40689}},\"count\":1}}"}
BgfCZw9eot4J4d: connection id 545942196.331978065 received message: {"event":"pusher:ping","data":{}}.
Connection id 545942196.331978065 sending message {"event":"pusher:pong"}
BgfCZw9eot4J4d: connection id 239518789.527591489 received message: {"event":"pusher:ping","data":{}}.
Connection id 239518789.527591489 sending message {"event":"pusher:pong"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions