Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@ng-idle/core

moribvndvs708.5kApache-2.016.0.0TypeScript support: included

A module for detecting and responding to the user becoming idle in Angular applications.

angular, angular2, ng2-idle, ng-idle, idle, inactive, timeout, session

readme

Introduction

Build Status Coverage Status

A module for responding to idle users in Angular applications. This is a rewrite of the ng-idle module; however if you are using Angular 1, you must use that module.

MAINTAINERS WANTED

The Angular community needs you! I'm looking for a new developer or team to take over maintenance of this module. These are the responsibilities any interested candidates should consider:

  • Now: Complete beta process (major remaining item is to make it compatible with SSR)
  • Now: Update demo and add API documentation
  • Ongoing: Bug fixes
  • Ongoing: New releases for new versions of Angular
  • Later: Refactor to simplify API and reduce package size
  • Later: Add support for non-browser environments?

Ideally, a candidate:

  • Has experience building applications in Angular 5+
  • Is an active Angular developer and tuned into the Angular release schedule
  • Loves open source and the Angular community
  • Is committed to releasing modular and lightweight (as possible) packages
  • Has working understanding of DOM events, JavaScript timers and intervals, Web Storage API, and cookies
  • Understands testing using Karma and Jasmine, and is committed to a high percentage of code coverage
  • Has working understanding of the contributing guide, is willing to accept contributions from others, and can use Github and related tools effectively
  • Has time to triage and answer tickets, or delegate to others
  • Has basic understanding of NPM for releasing packages

Please get in touch if you are interested!

Demo

Visit https://moribvndvs.github.io/ng2-idle to view a simple example with quick start instructions.

Quick start

@ng-idle is shipped via npm. You can install the package using the following command for the latest supported version of Angular:

npm install --save @ng-idle/core

Integrating and configuring the package into your application requires a few more steps. Please visit @ng-idle-example for source and instructions on how to get going.

Design Considerations

The primary application of this module is to detect when users are idle. It can also be used to warn users of an impending timeout, and then time them out. The core of this module is the Idle service which does its best - based on your configuration - to detect when a user is active or idle and pass that information on to your application so it can respond appropriately.

Modularization

The core functionality can be found in the @ng-idle/core package via npm.

Additional modules to extend functionality:

  • @ng-idle/keepalive (see below)

Extensible Keepalive Integration

In a common use case where it is used for session management, you may need to signal to the server periodically that the user is still logged in and active. If you need that functionality, @ng-idle can optionally integrate with @ng-idle/keepalive. @ng-idle will instruct @ng-idle/keepalive to ping while the user is active, and stop once they go idle or time out. When the user resumes activity or the idle state is reset, it will ping immediately and then resume pinging. Please note that keepalive integration is optional, and you must install and configure @ng-idle/keepalive separately to get this functionality. You can implement your own by extending KeepaliveSvc and configuring it as a provider in your application for the KeepaliveSvc class.

Extensible Interrupts

An interrupt is any source of input (typically from the user, but could be things like other tabs or an event) that can be used to signal to Idle that the idle watch should be interrupted or reset. Unlike ng-idle, these sources are not hardcoded; you can extend InterruptSource or any of the built-in sources to suit your purposes. This feature is also useful to handle input noise that may plague your particular use case. It can also be used to target specific elements on a page rather than the whole document or window. The following sources come built into this package:

  • InterruptSource (abstract): A base type you can implement to make your own source.
  • EventTargetInterruptSource: Any object that implements EventTarget, such as an HTMLElement or Window. Takes in the object that is the source and a space delimited string containing the events that cause an interrupt.
  • DocumentInterruptSource: Looks for events (in a space delimited string) that bubble up to the document.documentElement (html node).
  • WindowInterruptSource: Looks for events (in a space delimited string) that bubble up to the Window.
  • StorageInterruptSource: Looks only for the Storage event of Window object. Obligatory for LocalStorageExpiry.

NOTE: You must configure source(s) yourself when you initialize the application. By default, no interrupts are configured. You can use a configuration analogous to the ng-idle default by importing DEFAULT_INTERRUPTSOURCES and passing that reference to Idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);.

Extensible Expiry

Another feature ported from ng-idle is the ability to store an expiry value in some store where multiple tabs or windows running the same application can write to. Commonly, this store is the localStorage, but could be cookies or whatever you want. The purpose of this expiry and the expiry store is twofold: First, to prevent a window from not timing out if it sleeps or pauses longer than the configured timeout period. Second, it can be used so that activity in one tab or window prevents other tabs or windows in the same application from timing out.

By default, a LocalStorageExpiry type is provided, which will just keep track of the expiry in the localStorage. It will fulfill all purposes mentioned above. If you don't want to support multiple tabs or windows, you can use SimpleExpiry. In other words, SimpleExpiry does not coordinate last activity between tabs or windows. If you want to store the expiry value in another store, like cookies, you'll need to use or create an implementation that supports that. You can create your own by extending IdleExpiry or SimpleExpiry and configuring it as a provider for the IdleExpiry class.

Multiple Idle Instance Support

The dependency injector in Angular supports a hierarchical injection strategy. This allows you to create an instance of Idle at whatever scope you need, and there can be more than one instance. This allows you to have two separate watches, for example, on two different elements on the page.
If you use the default expiry (LocalStorageExpiry), you will need to define a name for each idle with Idle.setIdleName('yourIdleName'), otherwise the same key will be used in the localStorage and this feature will not work as expected.

Example Use Case

For example, consider an email application. For increased security, the application may wish to determine when the user is inactive and log them out, giving them a chance to extend their session if they are still at the computer and just got distracted. Additionally, for even better security the server may issue the user's session a security token that expires after 5 minutes of inactivity. The user may take much more time than that to type out their email and send it. It would be frustrating to find you are logged out when you were actively using the software!

@ng-idle/core can detect that the user is clicking, typing, touching, scrolling, etc. and know that the user is still active. It can work with @ng-idle/keepalive to ping the server every few minutes to keep them logged in. In this case, as long as the user is doing something, they stay logged in. If they step away from the computer, we can present a warning dialog, and then after a countdown, log them out.

Server-Side Rendering/Universal

@ng-idle/core uses DOM events on various targets to detect user activity. However, when using SSR/Universal Rendering the app is not always running in the browser and thus may not have access to these DOM targets, causing your app to potentially crash or throw errors as it tries to use browser globals like document and window through @ng-idle.

EventTargetInterruptSource and all the interrupt sources that derive from it (such as DocumentInterruptSource, WindowInterruptSource, and StorageInterruptSource) are designed to lazily initialize the event target listeners for compatibility with server-side rendering. The EventTargetInterruptSource will detect whether your app is running in the browser or on the server by using isPlatformServer and will skip initialization of the event target listeners when run on the server.

Developing

This project was developed using the NodeJS version found in the .nvmrc file. You may experience problems using older versions. Try NVM or similar to manage different versions of Node concurrently. If using NVM, you can execute nvm install to download and switch to the correct version.

Once you have cloned the repository, install all packages using npm:

npm install

You can now build and run all tests once with coverage.

 npm test

You can also continuously run tests while you make changes to a project by executing npm run ng test <project name> or ng test <project name> if you have @angular/cli installed globally.

npm run ng test core
...
npm run ng test keepalive

Note: Keepalive depends on Core. If you are running the above continuous tests, you'll need to npm build or npm run ng build core first and after making changes to Core. However, npm test will build all modules and run the tests in one shot.

Contributing

See the contributing guide.

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

16.0.0 (2024-09-18)

chore

BREAKING CHANGES

  • deps: requires Angular 18

15.0.0 (2024-06-21)

Features

BREAKING CHANGES

  • Requires Angular 17.3.10 or later, and requires Node 18 or later

14.0.0 (2023-11-09)

chore

BREAKING CHANGES

  • requires node 16+ and angular 16

13.0.1 (2023-10-25)

Bug Fixes

13.0.0 (2023-06-14)

Build System

  • deps: upgrade Angular dependencies to v15 (#186) (a5a7095)

BREAKING CHANGES

  • deps: requires Angular 15

12.0.4 (2023-05-18)

Note: Version bump only for package ng-idle

12.0.3 (2023-05-18)

Note: Version bump only for package ng-idle

12.0.2 (2023-05-18)

Note: Version bump only for package ng-idle

12.0.1 (2023-05-18)

Note: Version bump only for package ng-idle

12.0.0 (2022-08-06)

chore

BREAKING CHANGES

  • requires node 14+ and angular 14

11.1.0 (2021-09-30)

Features

  • interruptsource: support ssr with the default interrupt sources (#159) (f70fcad), closes #77 #115

11.0.3 (2021-07-18)

Note: Version bump only for package ng-idle

11.0.2 (2021-07-18)

Note: Version bump only for package ng-idle

11.0.1 (2021-07-18)

Note: Version bump only for package ng-idle

11.0.0 (2021-07-18)

BREAKING CHANGES

  • This version requires Angular 9 or later.

10.0.0 (2020-09-10)

Features

  • project: upgrade to angular 10 (ea853a6)

BREAKING CHANGES

  • project: required angular 10

9.0.0-beta.1 (2020-07-01)

Features

  • angular dependency: update to angular 9.1.6 (4379af2), closes #139

BREAKING CHANGES

  • angular dependency: required angular 9

8.0.0-beta.4 (2019-07-23)

Note: Version bump only for package ng-idle

8.0.0-beta.3 (2019-07-23)

Note: Version bump only for package ng-idle

8.0.0-beta.2 (2019-07-23)

Note: Version bump only for package ng-idle

8.0.0-beta.1 (2019-07-23)

Bug Fixes

  • idle: ensure countdown short circuits if user returns to active (45325d3)
  • public-api: retain parity with exports from old index (bd172cf)

BREAKING CHANGES

  • deps: update to Angular 8

7.0.0-beta.1 (2019-05-09)

Chores

  • deps: update to Angular 7

BREAKING CHANGES

  • deps: This version is only compatible with Angular 7. If you are still using Angular 6, use @ng-idle@6.0.0-beta.5 or upgrade to Angular 7.

6.0.0-beta.5 (2019-04-04)

Bug Fixes

  • keepalive: Run keepalive setInterval outside NgZone (04e3d4c), closes #113

6.0.0-beta.4 (2019-03-19)

Bug Fixes

  • keepalive: return actual response or error in onPingResponse (0741ae2), closes #118

6.0.0-beta.3 (2018-07-16)

Bug Fixes

  • core: check key on storage event argument exists (2fc712d)

6.0.0-beta.2 (2018-05-31)

Bug Fixes

  • InterruptSource: remove extraneous zonejs import (a49e1af), closes #99 #101

6.0.0-beta.1 (2018-05-24)

Note: The version scheme for this module has been changed to match the version of Angular it targets. Use an @ng-idle@2.0.0 build if you are using Angular 5.

Chores

  • deps: update to Angular 6 and RxJs 6.1 (4a71953), closes #93 #94

BREAKING CHANGES

  • deps: This version is only compatible with Angular 6.

2.0.0-beta.15 (2018-03-29)

Bug Fixes

  • E2E tests in Protractor: Change to setInterval to run outside of angular zone (d8599d7)
  • Unit Tests: Change to restrict zone.js version (93258e2)

BREAKING CHANGES

  • E2E tests in Protractor: idle constructor has additional paramter

2.0.0-beta.14 (2018-03-29)

Bug Fixes

  • InterruptSource: attach outside Angular (c94fc9a)

2.0.0-beta.13 (2017-12-17)

Chores

Features

  • EventTargetInterruptSource: constructor takes options that include flag for using passive even (a5e24eb)

BREAKING CHANGES

  • deps: Support for Angular 2.x dropped due to missing @angular/common/http dependency

2.0.0-beta.12 (2017-05-30)

Bug Fixes

  • idle: use expiry.now() if expiry.last() returns null (06fcb36), closes #54

2.0.0-beta.11 (2017-04-14)

Refactor

  • updated imports of rxjs to only import the operators and types used.

2.0.0-beta.10 (2017-03-30)

Chores

  • deps: support Angular 4 in addition to Angular 2

2.0.0-beta.9 (2017-03-20)

Bug Fixes

  • idle: check expiry on timeout doCountdown (e23233d)

2.0.0-beta.8 (2017-02-20)

Bug Fixes

  • Initialize localStorage idling value to false in constructor (ab71433)

2.0.0-beta.7 (2017-02-16)

Bug Fixes

  • core: check expiry before toggling idle state (6702c36), closes #37

2.0.0-beta.6 (2017-02-14)

Bug Fixes

  • AutoResume.notIdle with LocalStorageExpiry wasn't working well (935ade9)
  • bugfix when last() with null value throws exception (5b3d175)

2.0.0-beta.5 (2017-02-09)

The new default expiry is now LocalStorageExpiry, and StorageInterruptSource has been added to the default module providers. This in effect makes idle coordination between tabs the new default (rather than SimpleExpiry). This makes the defaults more like the original ng-idle. You can of course override the IdleExpiry provider and go back to SimpleExpiry or use your own. Special thanks to @rousseaufiliong for porting that functionality.

Features

  • prevent timing out one tab or window if another tab have activity (#33) (3ab086d)

2.0.0-beta.4 (2016-12-02)

License

  • LICENSE change license to Apache-2.0 (f8caa10)

2.0.0-beta.3 (2016-11-21)

Features

2.0.0-beta.2 (2016-11-15)

Bug Fixes

  • core, keepalive: emit helpers for es5 compatibility (ed30b6b)

2.0.0-beta.1 (2016-11-14)

Code Refactoring

Features

  • keepalive: merge ng2-idle-keepalive into @ng-idle/keepalive (7702d9d)

BREAKING CHANGES

  • keepalive: Modules have been changed from Ng2IdleModule and Ng2IdleKeepaliveModule to NgIdleModule and NgIdleKeepaliveModule, respectively. Additionally, keepalive imports should now be @ng-idle/keepalive.
  • core: This repository has been refactored to follow the @angular model of modularization so that it can be maintained more easily. You will need to change your import statements from 'ng2-idle' to '@ng-idle/core'.

1.0.0-alpha.18 (2016-11-04)

Features

BREAKING CHANGES

This change removes the core.ts. Instead, you should create an app module using NgModule and import the new Ng2IdleModule.

  • Change any import ... from 'ng2-idle/core'; to import ... from 'ng2-idle';
  • Create an AppModule using NgModule if you haven't already. NgModule was added in Angular2 RC.5
  • Use Ng2Module.forRoot() in your app module's imports declaration.

Example:

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { Ng2IdleModule } from 'ng2-idle';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    Ng2IdleModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';

import {Idle, DEFAULT_INTERRUPTSOURCES} from 'ng2-idle';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private idle: Idle) {
    // initialize idle here.
  }
}

1.0.0-alpha.17 (2016-11-02)

Chores

  • deps: relax angular dependencies to minor version

1.0.0-alpha.16 (2016-09-19)

Chores

1.0.0-alpha.15 (2016-08-23)

Bug Fixes

  • release:: incorrect publishing

1.0.0-alpha.14 (2016-08-23)

DEPRECATED

Chores

  • deps: update to angular 2.0.0-rc.5

1.0.0-alpha.13 (2016-06-30)

Chores

  • deps: update to angular 2.0.0-rc.4

1.0.0-alpha.12 (2016-06-22)

Update to Angular 2.0.0-rc.3

1.0.0-alpha.11 (2016-05-09)

BREAKING CHANGES

This release targets Angular2 RC.1. Angular2 RC.0 introduced breaking changes to how Angular is packaged. You will need to upgrade your application to RC.0 or later for this version of ng2-idle to work.

1.0.0-alpha.10 (2016-04-06)

Bug Fixes

  • Idle: do not pause interrupts when idle (d8d600d), closes #4 #5

1.0.0-alpha.9 (2016-03-25)

Bug Fixes

  • typings: add ambient dev dependencies for es6-shim (1a6fe2a)

1.0.0-alpha.8 (2016-03-25)

Bug Fixes

  • dependencies: update to target Angular 2.0.0-beta.12 (37bf7ad)

1.0.0-alpha.7 (2016-02-24)

Features

  • EventTargetInterruptSource: target events can be throttled over a specified duration (174d2d0)

Performance Improvements

  • Idle: interrupts are detached when state is idle or not running (e0b2a1e)

1.0.0-alpha.6 (2016-02-16)

Features

  • IdleExpiry: add extensible timeout expiry support (31be875)

1.0.0-alpha.5 (2016-02-16)

Bug Fixes

  • Idle: immediately ping and resume keepalive upon interrupting idle state (62b47b2)

1.0.0-alpha.4 (2016-02-16)

Bug Fixes

  • EventTargetInterruptSource: subscribing to multiple events separated by space no longer breaks (c552d07)
  • Idle: setKeepaliveEnabled(false) no longer stops keepalive (050a590)

1.0.0-alpha.3 (2016-02-16)

Bug Fixes

  • Build: exclude postinstall task when installing production (1db53ca)

1.0.0-alpha.2 (2016-02-16)

Features

  • Keepalive: implements optional keepalive integration (5a727dd)

1.0.0-alpha.1 (2016-02-11)

Features

  • Idle: add service with basic features (6db5d50)