Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect a user is offline when using liveQuery in a realtime chat app #1906

Closed
westhacker opened this issue May 25, 2016 · 9 comments · Fixed by #7043
Closed

How to detect a user is offline when using liveQuery in a realtime chat app #1906

westhacker opened this issue May 25, 2016 · 9 comments · Fixed by #7043
Labels
type:feature New feature or improvement of existing feature

Comments

@westhacker
Copy link

I am using live queries to realize the realtime chat app, but I could not find the method to detect a user is online or offline. I am also confused with the enter and leave events.

subscription.on('enter', (object) => {
  console.log('object entered');
});

When an existing ParseObject's old value does not fulfill the ParseQuery but its new value fulfills the ParseQuery, you'll get this event. The object is the ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.

subscription.on('leave', (object) => {
  console.log('object left');
});

When an existing ParseObject's old value fulfills the ParseQuery but its new value doesn't fulfill the ParseQuery, you'll get this event. The object is the ParseObject which leaves the ParseQuery. Its content is the latest value of the ParseObject.

I wonder when to trigger these two events. Thanks in advance!

@drew-gross
Copy link
Contributor

Lets say your Live Query is finding objects that have the online field set to true. If an object is created with the online field set to true, you will get the create event. If it is created with the online field set to false and then later, the the online field is set to true, you will get the enter event.

leave is like the reverse of enter

@westhacker
Copy link
Author

@drew-gross Thanks very much for your reply.

Is it possible to set online field to false automatically when a user quits the chat app ( The socket connection is closed when user quit the chat app ) ?

@drew-gross
Copy link
Contributor

No, although that does sound like a good use case that we may want to think about adding. In the meantime, my recommendation is to use a separate save in the quit function of your chat app, or if thats not possible, consider tracking idle time of a user and mark them offline/idle after they don't do anything for awhile.

@febg
Copy link

febg commented Nov 1, 2019

@drew-gross did you guys ended up adding any functionality for online/offline users? I need to detect user presence for a chat application. Do you have any recommendations on how to do it form a web application. If a user closes the browser window for example, I havent found a way to detect the user is gone.

@Etto91
Copy link

Etto91 commented Apr 20, 2020

i tried to implement online/offline with the onLiveQueryEvent method. with the connect event i can save the client id with the user but when i close the tab, ws_disconnect event is triggered without the client id. i also tried with installationId but with multiple tabs it doesn't change.

is it possible to retrieve the client id when the ws_disconnect event is triggered?

Screenshot 2020-04-20 at 22 55 40

@kevin4dhd
Copy link

Any solution?

@maxiqsoft
Copy link

I found a solution.

I've added one line of code to the file /node_modules/parse-server/lib/LiveQuery/ParseLiveQueryServer.js

The line is sessionToken: client.sessionToken, and in my case in line 459.

With that you are able to recognize who disconnected from the websocket by the users sessionToken.

Before it was just possible to get the sessionToken when the user connected.

With that you can make a function to get the user object from the Session class and save it maybe in an array.

This is where I added the line:


(0, _triggers.runLiveQueryEventHandlers)({
        event: 'ws_disconnect',
        clients: this.clients.size,
		sessionToken: client.sessionToken,
        subscriptions: this.subscriptions.size,
        useMasterKey: client.hasMasterKey,
        installationId: client.installationId
      });

@mtrezza
Copy link
Member

mtrezza commented Dec 3, 2020

Thanks for the update.

I reopened the issue because it seems like a useful and much sought after enhancement.

If I understand correctly, it would currently be possible to infer the user from the installationId, but that is indirect and requires additional referencing. This solution on the other hand would allow to infer the user directly from the session object.

@maxiqsoft Would you want to open a PR for this?

@maxiqsoft
Copy link

maxiqsoft commented Dec 3, 2020

Sorry, I am too dumb to push the changed code to the repo. I become the message that my local git version is too old and I am currently in homeoffice, so no admin rights.

The file that has to become changed is located here: parse-server\src\LiveQuery
the name of the file: ParseLiveQueryServer.js at line 420

runLiveQueryEventHandlers({ event: 'ws_disconnect', clients: this.clients.size, subscriptions: this.subscriptions.size, useMasterKey: client.hasMasterKey, installationId: client.installationId, });

should become to
runLiveQueryEventHandlers({ event: 'ws_disconnect', clients: this.clients.size, subscriptions: this.subscriptions.size, sessionToken: client.sessionToken, useMasterKey: client.hasMasterKey, installationId: client.installationId, });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants