laravel-websockets icon indicating copy to clipboard operation
laravel-websockets copied to clipboard

Extend PusherChannelProtocolMessage ping() method

Open lucianobosco opened this issue 2 years ago • 0 comments

I'm tracking user's status as per websocket by having a participants DB table id | user_id | socket_id | created_at | updated_at

When a user joins a presence channel the socket_id is persisted in participants table such as 1 | 365 | 251049992.788979155 | 2021-10-05 | 2021-10-05

When a user left a presence channel the socket_id is set to null 1 | 365 | null | 2021-10-05 | 2021-10-05

By leaving a presence channel doesn't mean that the user is offline, in fact, the user can accidentally close the browser/tab and open it again, therefore to know if user is offline what I do is to prune models having not socket_id and a updated_at date older that 5 minutes.

What I'm trying to achieve here is to keep the updated_at column up-to-date on every ping/pong WebSocket does. Digging a little bit in vendor files I found the ping() method within PusherChannelProtocolMessage class. Saying that I tried something that worked as expected:

protected function ping(ConnectionInterface $connection)
    {
        // Update updated_at column in participant model
        $participant = Participant::where('socket_id', $connection->socketId)->find();
        if ($participant) {  $participant->touch(); }

        $connection->send(json_encode([
            'event' => 'pusher:pong',
        ]));
    }

Now the question is: how can I extend PusherChannelProtocolMessage class to overwrite the ping() method in my whole app? Thanks in advance

lucianobosco avatar Oct 21 '21 20:10 lucianobosco