<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sami BOUYAT on Medium]]></title>
        <description><![CDATA[Stories by Sami BOUYAT on Medium]]></description>
        <link>https://medium.com/@sbouyaahmed?source=rss-21daba1bcfef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wYlFIyfDF_8pdiWCwJljPA@2x.jpeg</url>
            <title>Stories by Sami BOUYAT on Medium</title>
            <link>https://medium.com/@sbouyaahmed?source=rss-21daba1bcfef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 17:39:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sbouyaahmed/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular ng-idle TimeOut]]></title>
            <link>https://medium.com/@sbouyaahmed/angular-ng-idle-timeout-7883509578af?source=rss-21daba1bcfef------2</link>
            <guid isPermaLink="false">https://medium.com/p/7883509578af</guid>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Sami BOUYAT]]></dc:creator>
            <pubDate>Sun, 03 Jul 2022 23:39:34 GMT</pubDate>
            <atom:updated>2022-07-03T23:39:34.681Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*aHtteePGhw1DlzYFnN-cnA.png" /></figure><p>Idle timeout is mostly needed in web apps for two reasons: security and to avoid unnecessary calls to the API.</p><p>Let’s see how we can implement the idle timeout feature in the Angular application with the ng-idle library.</p><h4>Implementation</h4><p>First of all, you need to install ng-idle dependencies in the existing application.</p><blockquote>npm install — save @ng-idle/core @ng-idle/keepalive angular2-moment</blockquote><p>Once these modules are installed, you need to import them to the app.modules.ts</p><pre>import { NgIdleKeepaliveModule } from &#39;@ng-idle/keepalive&#39;; <br>@NgModule({  declarations: [  ],  <br>imports: <br>[    NgIdleKeepaliveModule.forRoot(), <br>   MomentModule,  ],  <br>providers: [],  <br>bootstrap: [AppComponent]})<br>export class AppModule { }</pre><p>Since AppComponent is the main/bootstrap component in the Angular application, we need to implement this logic in it.</p><p>in this example, we are going to set the idle time to 10 seconds wich means we will get a warning message after 10 seconds of inactivity, and timeout as 10 seconds too , wich means it will be timed out within 10 another seconds .</p><p>in real app , these times are longer in the actual environment.</p><pre>import { Idle, DEFAULT_INTERRUPTSOURCES } from &#39;@ng-idle/core&#39;;<br>import { Keepalive } from &#39;@ng-idle/keepalive&#39;;</pre><pre>@Component({  selector: &#39;app-root&#39;,  <br>templateUrl: &#39;./app.component.html&#39;,  <br>styleUrls: [&#39;./app.component.css&#39;]})</pre><pre>export class AppComponent {</pre><pre>idleState = &#39;Not started&#39;; <br>timedOut = false;  <br>lastPing?: Date = null; <br>title = &#39;angular-idle-timeout&#39;;</pre><pre>constructor(private idle: Idle, private keepalive: Keepalive) {</pre><pre>// sets an idle timeout of 10 seconds, for testing.    idle.setIdle(10);</pre><pre>// sets a timeout period of 10 seconds. after 20 seconds of inactivity, the user will be considered timed out.<br> idle.setTimeout(5);</pre><pre>// sets the default interrupts, in this case, things like clicks, scrolls, touches to the document</pre><pre>idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);</pre><pre>idle.onIdleEnd.subscribe(() =&gt; {       <br>this.idleState = &#39;No longer idle&#39;      <br>this.reset();    <br>});</pre><pre>idle.onTimeout.subscribe(() =&gt; {<br>      this.idleState = &#39;Timed out!&#39;;<br>      this.timedOut = true;<br>      console.log(this.idleState);<br>      this.router.navigate([&#39;/&#39;]);<br>    });</pre><pre>idle.onIdleStart.subscribe(() =&gt; {<br>        this.idleState = &#39;You\&#39;ve gone idle!&#39;<br>        console.log(this.idleState);<br>        this.childModal.show();<br>    });<br>    <br>    idle.onTimeoutWarning.subscribe((countdown) =&gt; {<br>      this.idleState = &#39;You will time out in &#39; + countdown + &#39; seconds!&#39;<br>      console.log(this.idleState);<br>    });</pre><pre>// sets the ping interval to 15 seconds<br>    keepalive.interval(15);</pre><pre>  keepalive.onPing.subscribe(() =&gt; this.lastPing = new Date());</pre><pre> this.reset();<br> }</pre><pre>reset() {  <br>  this.idle.watch(); <br>  this.idleState = &#39;Started&#39;; <br>  this.timedOut = false;  <br>}</pre><h3>Summary</h3><ul><li>Idle timeout is one of the common features in every web application yet a very important one.</li><li>We can avoid malicious attacks and unnecessary backend API calls with this feature.</li><li>We can actually detect the idle user with the help of DOM events: keyboard events and mouse events.</li><li>For Angular applications, we can use the ng-idle library.</li><li>We can conditionally watch and unwatch the user with the help of idle.watch and idle.start methods from ng-idle lib.</li><li>It’s always good to let the user know with the modal popup that he/she has been idle before logging them out.</li></ul><h3>Conclusion</h3><p>idle timeout is more common and powerful to secure app from malicious attakcks and unnecessary Http calls .</p><p>For more details :</p><pre>|<a href="https://www.npmjs.com/package/@ng-idle/core">https://www.npmjs.com/package/@ng-idle/core</a> </pre><pre><a href="https://www.npmjs.com/package/@ng-idle/keepalive">https://www.npmjs.com/package/@ng-idle/keepalive</a></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7883509578af" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>