Laravel - Pusher - Detect when a user is no longer in the website or leaves the page

安稳与你 提交于 2019-12-24 00:07:21

问题


I'm facing a problem with a real time app that i'm developing. My problem is about user's status using Pusher. I would like to get the correct way to detect when a user leaves the page or keep unactive for a while to set it's status to 'offline', for example. How could I achieve that? I'm almost sure that is not recommended develop this feature at client-side, it should be at server-side, but how? How can I ask to clients if they are still there? Should I create a command and run it wit cron? Are there any mechanism to let the server 'detect' the socket disconnection? Any help will be appreciated, Thx!

Update

With NoseJS and SocketIO it's really simple, you just have to write:

io.on('connection', function (socket) { // When client connects
    socket.on('disconnect', function() { // Listen to disconnections from SERVER-SIDE
        // Logic when a user disconnects. This is what I want!
    });
});

But in Laravel, I only have this:

// A Laravel Event Class that implements the shouldBroadcast interface...
    public function broadcastOn()
    {
        // I think here is where connections happen, but I don't know how to detect 'disconnections' :(
        return new PresenceChannel('someString');
    }

PD: I'm using Laravel Echo and Pusher :)


回答1:


You can use presence channels to achieve this. You can then use presence webhooks to notify your server whenever a user enters or leaves the channel.



来源:https://stackoverflow.com/questions/46927831/laravel-pusher-detect-when-a-user-is-no-longer-in-the-website-or-leaves-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!