idle.ts is a small typescript / javascript lib that helps you indicate whether a user is active or idle. You can use this to get events about the user activitness, and create your own actions - log activity, sign idle users out, show warning modal, etc. The library also offers smart batching with optimal balance between accuracy and effiency.
npm install idle.ts
Or copy idle.ts (for typescript) or idle.js (for javascript) to your location
Use attached Jasmine file for testing.
Start listening to user activity by calling
let idle = Idle(...)
activityReportInSec?: number;
The number of seconds interval a report about user activity is sent.activityReportMode?: ReportMode
see Smart BatchingdetectFramesInSec?: number;
Frames can be added dynamicaly after the page was already created. If the page you inpect creates frames after loaded set the interval time, in which the dicovery frames code may run. By default, it only runs once the page is loadedonActivity?: ()=>void;
a event called when a user is starting to be active after being idleonEnterFrame?: ()=>void;
a event called when a user enters a frame and listener can't listen to any page eventonReportUserIsActive?: ()=>void;
a periodic report states that the user was being active on the last interval checked.onReportUserIsIdle?: ()=>void;
a periodic report states that the user was being idle on the last interval checked.
In order to reduce load on the server we don’t want to call the refresh cookie url, upon every event that we catch. We should check if the user made an activity over X minutes, and call the endpoint if he was active. So how do we set this X interval to the optimal time?
- If we make it to long - we can lose events. A user can be active and close the browser / disconnect from the internet, before activity report was sent
- If we make it too short we can overload the server
send report every 5 minutes if user was active
upon user activity - raise flag every 5 minutes - if flag is raised, call endpoint to refresh cookie. drop flag.
User makes an operation and closes the browser before 5 minutes elapsed and cookie is not refreshed “Real world” scenarios (derived from the edge case):
- Admin sets the idle timeout to 30 minutes.
- User opens a page.
- User is inactive for 15 minutes.
- User makes an operation and closes the page.
- 30 minutes elapsed and user is kicked out, despite of their activity.
- Admin sets the idle timeout to 30 minutes.
- User opens a page.
- User is inactive for 27 minutes.
- User goes back to the page, and does many operations.
- 30 minutes elapsed and user is kicked out, while active.
upon user activity - if flag is dropped, call endpoint to refresh cookie and raise flag every 5 minutes - drop flag
User makes an operation and closes the browser before “the first operation after 5 minutes” and cookie is not refreshed “Real world” scenarios (derived from the edge case): User can be Session can be shorter than expected:
- Admin sets team’s timeout to 10 minutes
- User makes an operation on minute 00:01. Cookie is refreshed.
- User makes an operation on minute 04:59. Cookie is not refreshed.
- On minutes 10:01 user session expires, though user was only idle for 5:01 minutes
Session can be shorter than the expected idle timeout by the interval time (see the table above for the interval times we set for each preset timeout). For example: if the admin sets the idle timeout to 8 hours, users should be kicked out after 7.5-8 hours (30 minutes interval)