Pablo
Pablo

Reputation: 1471

Accounting for users that have left website without using onunload

I have a webservice with very limited resources (I will be able to handle about 3 simultaneous users).

When users interact with my website they start a complex process server-side. (This process is the limiting factor, as my server machine will not be able to handle many in parallel, and clients cannot run this on their side.)

My question is how to make sure to end the process for users that leave, for example by closing the window.

I have considered onunload and onbeforeunload, but they are also triggered by links within the website (which I need for users to be able to interact with the process) so that does not seem like an option.

This approach seems problematic according to other questions (see this, for example), but it could work if there were a way to check if the user is still an active user when performing the action triggered by onunload (even if in a different page of the website), but I don't know how to do this.

I have also considered periodically checking the list of active users and cancelling the process for users that have left, but I don't know if this is even possible.

I have zero experience with cookies, but could this be a place to use them? Can the server access the (still living) cookies of disconnected users?

Which sounds like a reasonable approach for this problem?

Upvotes: 0

Views: 30

Answers (1)

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5659

Cases such as these are generally handled by heartbeats. Have your client send periodic heartbeats (which are essentially pings) to the server notifying that it is still alive and interested in the process's results. And the server automatically kills those processes for which it hasn't received client heartbeat for a configured amount of time.

I have considered onunload and onbeforeunload

You are right- you can't rely on them.

I have zero experience with cookies, but could this be a place to use them?

No. Cookies maintain client-side state that is sent to a server on HTTP calls. So, servers don't manage cookies. Instead, they only look at them to identify state.

Upvotes: 1

Related Questions