Start a new topic

User presence tracking

I want to indicate to users how many other users are on a document, and so I need a way of knowing "when a user leaves the page".

[This blog post]( "This blog post") explains how firebase does it - does kinvey have similar functionality?

Not out of the box, but this is something that can be easily implemented. Right after your `Kinvey.init` call, set `Kinvey.getActiveUser().lastOnline = new Date();`, and call save on the active user.

Now that all users have a `lastOnline` property, you can query the user collection for users online right now. Depending on your use case, you can adjust the time query to whatever works for you. The query would be similar to:


var threshold = new Date().getTime() - 5 * 60 * 1000;// Users online in the last 5 minutes.

var query = Kinvey.Query().greaterThan('lastOnline', new Date(threshold).toISOString());


The result of the query will list all users online in the last five minutes.
Hmm I was hoping for something a little more real-time than that -- my use case is multiple people editing a document (google docs style)
In that case, I am assuming you are sending a request to Kinvey every time the document is updated (i.e. the user makes a change)? You could use a collection hook to update the `lastOnline` time right then, that will give you a more precise estimate. That’s the closest to real time I can think of.
The last option is to use polling, hit a custom endpoint that just updates the `lastOnline` status every 10 seconds or something. You could even have that endpoint return who is online within the last 20 seconds or something like that.

As of today there is no way to get push status changes on user activity ala websockets with Kinvey. You are either going to have to poll or use push notifications (if on mobile).
Ok cool thanks
Login or Signup to post a comment