Track user session length and make a decision

Does anyone have any examples how to track the current user session length and make a decision after x amount of minutes or hours have passed?
4 answers

Hey Alex.

If it’s a plain “stop the session” decision you want to add, you can edit the Mendix Runtime setting " SessionTimeout” to another maximum time.

If it’s more custom, my approach would be as follows:
Create a microflow with a logged in user as parameter, and retrieve said user’s session(s). Now you have access to 3 attributes relevant to your question: The user’s “Last login” and session’s “last active” and “created date” (last one is technically not necessary to use if the user doesn’t log into multiple sessions at once, use it if you think that can be the case).
Now in your microflow, you can start by checking the current time of day with [%CurrentDateTime%] and comparing it to the last login of the user (or session created date if it’s multiple sessions). If it exceeds your treshhold, then you can follow up with your custom logic.
Same deal with the session “last active”, if your user has been idle for a longer time than you want him to, but not make his session time out.

Finally, to keep constant track of this, you can decide to make a scheduled event and set it to a relatively short timer (within reason. Don’t slow down the user experience with background processes too much). Then just attach said microflow to that.
If it’s a check that doesn’t depend as much on a timer, or idle’ing, you could also attach the microflow to any page opening microflows you have, which would remove the need for an SE.

  1. Use $currentSession
  2. Use createdDate attribute along with [%CurrentDateTime%] for comparison

Thank you for answering Gaston!

I am actually fine not using Java if its not necessary. 

This is what I have so far 

I create a time variable and then I retrieve all the sessions and compare with the time variable. If session is older than X hours, then I add them to the sessionList. The problem I have is that now I have to pass this variables to a java action call and use them to log out the user(the main problem I have is that I am also using SSO, so not only I have to logout the user from mendix, I also have to trigger a open.url() call. ) I am basically going down a rabbit hole with this approach.


Would you be able to screen shot your microflow example? I am pretty new to Mendix and I dont even know how to trigger a microflow as logged in user. 


Hey Alex,

As for a basic microflow structure, you could go for something like this. This one assumes a single session due to its simplicity, but it gives you the gist to expand on it.

The HourTreshold is just an integer.
SessionCreated retrieves when the session was started:
The decision is a datetime comparison method built in by mendix, HoursBetween:
  hoursBetween( $SessionCreated , [%CurrentDateTime%] ) > $HourTreshold

When that returns true, you can then do whatever you wish after there. In this case here, it calls the Administration microflow for logging out of a session.
As far I know you can’t build a URL redirect/ open link within a microflow, you will need java for that. A java action call would suffice there.

To make that automatically happen during your application’s runtime, I don’t think you can avoid something like a Scheduled Event. Seeing you are new to Mendix I would suggest you read through their documentation, it’s pretty straight forward but a bit of a mouth full to put in a forum post. Then you can put your entire session list you already know how to retrieve within a microflow’s loop as its iterator, and use the basic decision example right in there.

Hopefully this helps you out.