0

I have a web application made in Code igniter. What it basically does is -

  1. user logs in to the application
  2. User can do various tasks, one of which is timer based tasks. There is a button which starts the javascript timer
  3. While timer is running he performs certain tasks, and once he is done, he stops the timer. The timer may run for few minutes to few hours.

My Question - While JavaScript timer is running, can we prevent session timeout, so that users are not logged out and their progress is not lost?? My initial thought is that it can't be, at least there isn't an easy way because we are dealing with Javascript timer (Client) and the Session timeouts (server).

Thanks in advance

WatsMyName
  • 4,240
  • 5
  • 42
  • 73
  • Are you doing a `poll` through client side ? – Rayon May 20 '16 at 06:21
  • While the timer is up poll your server to keep session alive – Anupam May 20 '16 at 06:22
  • @Rayon, its a application for patient with some disorder. For example, Like i have habit of blinking eyes too frequent and I would like to stop this. For this I run the timer and I try to refrain myself from blinking my eyes as long as I can. When I blink i stop the timer and note the total minutes. Next time I try to hold this blinking even more. – WatsMyName May 20 '16 at 06:27
  • @WatsMyName, How does your client side understand that session is time out ? – Rayon May 20 '16 at 06:29
  • @Rayon, We have no ajax calls while timer is running. Client side doesn't know session is time out. But in codeigniter , we have `$config['sess_expiration']` set to 1 hour. Lets say user is running a timer, and while running in server session gets timeout, after sometime user stops the timer, which also saves data to the database. Since session is already time out, while saving we will have no user informations like user ids, etc in sessions. – WatsMyName May 20 '16 at 06:32
  • @WatsMyName, One your client timer has started, Init an ajax with `$config['sess_expiration']= 0;` which will make sure _Session will not expire_ and once it is done, reset it to initial value.. Makes sense ? – Rayon May 20 '16 at 06:35
  • @Rayon I think this is the only way. – WatsMyName May 20 '16 at 06:37
  • @Rayon: In your solution you are asking javascript to not kill session, right? but what if serverside session is expired? – Parag Bhayani May 20 '16 at 06:56
  • @ParagBhayani, Using ajax, I am setting `$config['sess_expiration']= 0;` to prevent server side side session timeout... – Rayon May 20 '16 at 06:56
  • @Rayon: Ohkey, and your syntax are specific to CodeIgniter? or is it generic? – Parag Bhayani May 20 '16 at 06:59
  • @ParagBhayani, `$config['sess_expiration']= 0;` is specific for CI.. I never worked on CI but some sources suggested this syntax.. – Rayon May 20 '16 at 07:00
  • @Rayon: Thanks a lot :) – Parag Bhayani May 20 '16 at 07:14
  • @ParagBhayani, Pleasure :) – Rayon May 20 '16 at 07:15
  • @ParagBhayani, I m doing so, accidently unselected the answer. – WatsMyName May 25 '16 at 09:58

1 Answers1

4

Throughout the timer session keep sending keepalive request every some minutes(preferred 2 to 5 minutes)

This keepalive request will be the as light as possible and will only get session on the server and will keep it alive

The response will be also as simple as something like session is still active kind of thing...

On the other hand you could also maintain a variable on javascript side usersLastActivity, which is updated on each document mousemove or document keydown and few events. If there's been any activity since last request, then send keepalive request ...

To get more idea about you can have a look at other same kind of question posted here.

The basic example:

setInterval(function(){
   $.get('/ImStillAlive.action');
}, 300000); // 5 mins * 60 * 1000

With basic check for typing activity:

$(function(){
    var lastUpdate = 0;
    var checkInterval = setInterval(function(){
       if(new Date().getTime() - lastUpdate > 300000){
           clearInterval(checkInterval);
       }else{   
            $.get('/ImStillAlive.action');
       }
    }, 300000); // 5 mins * 60 * 1000

    $(document).keydown(function(){
         lastUpdate = new Date().getTime();
    });
});
Community
  • 1
  • 1
Parag Bhayani
  • 3,280
  • 2
  • 28
  • 52