Trigger an action when the activity status of the user changes.
- Node.js >= 8.0.0
npm install afk
This module uses a native module that needs to be built using
On linux you will need to install
pkg-config to ensure that the native module dependency can be built.
const NodeAFK = ;const inactivityDuration = 1000 * 10; // the user will be considered `idle` after 10 secondsconst afk = inactivityDuration;afk;afk;afk
node-afk is an event emitter and emits the following events:
status:idle- the status of the user changed from
status:active- the status of the user changed from
status-changed- the status of the user changed. An object is passed to the listener for this event containing details of the previous and current status.
error- an error occured
You can unregister a listener from an event using the
off method of the event emitter:
You can also setup a listener that is executed when the status of the user has been their current status for a certain duration:
These events will be emitted each time the status of the user is changed.
If an error occurs whilst trying to retrieve the idle time from the system an
error event will be emitted:
Create a new instance of
inactivityDuration- How long (in
ms) until the user can be inactive until they are considered as
pollInterval- How often (in
node-afkquery the system to get the the amount of time that the user has been away for (
Register a listener on an event
listener- Function to be executed when the event is emitted
Unregister a listener from an event
eventName- The name of the event
listener- The listener associated with the event
This is required to be called so that the poll interval is setup.
Stops the poll interval and removes all event listeners.
Contributions are always welcome. Make sure you write tests for anything you add or change. We also enforce AirBNB ESLint rules.