[Session Issue] session.userData.lastAccessTime seems not able to searlize inside the setTimeout function
I've the following code proactively check if any user input has been idle for certain time, then close the bot session but find that the access time storing in the session.userData can't be updated inside setTimeout function. Here's is the code
bot.use({
botbuilder: function (session, next) {
var date = new Date()
session.userData.lastAccess = date.valueOf()
console.log("Current acess time: ", session.userData.lastAccess)
setTimeout(function () {
console.log("Last user access time: ", session.userData.lastAccess)
var idleTime = Date.now() - lastAccessTime
var shouldExpire = (session) => idleTime > 30000
if (shouldExpire(session)) {
session.endConversation('Session idle over 30 sec. Session end....' + idleTime)
session.clearDialogStack()
session.reset()
session.userData.firstRun = false
} else {
console.log("idleTime less than max of 30 sec, keep the session")
}
}, 30000)
Expected Behavior
If I've 2 user inputs, I should have got the session.userData.lastAccess overwritten by the 2nd last access time which is 1494162055469 Current access time: 1494162049089 Current access time: 1494162055469
But when the 2 setTimeout functions time up and checking the session.userData.lastAccess time, I got the following instead? Last user access time: 1494162049089 Last user access time: 1494162055469
What's wrong with the session.userData inside the setTimeout function? I expect both should last access time should be 1494162055469, not an individual copy. Can anyone point me out. please
@wtam are you using C# or Node SDK?
Hi Nils,
I'm using nodejs sdk
Regards, William
@wtam To get the user's lastAccess time, use the built-in variable session.sessionState.lastAccess instead.
It's has the same problem and even worst, as sessionState.lastAccess will return the same timestamp in the 1st and 2nd call. And when the setTiemOut timeup, it has 2 copies of the of the lastAccess tiemstamp like what I have in the userData
I believe it's the the problem of the session not able to searailize? or referencing issue!. But if I replace the session to a global variable, "lastAccessTime", I can solve it but I need its per user session not as a global state. Please help how to get the session state update inside the setTimeout function.
You can try this simple code and see it yourself var lastAccessTime = null bot.use({ botbuilder: function (session, next) { session.userData.lastAccess = Date.now() lastAccessTime = session.userData.lastAccess console.log("userData.lastAccess: ", session.userData.lastAccess, " sessionState.lastAccess: ", session.sessionState.lastAccess, "session.lastSendTime", session.lastSendTime, "lastAccessTime: ", lastAccessTime) setTimeout(function () { console.log("userData.lastAccess: ", session.userData.lastAccess, " sessionState.lastAccess: ", session.sessionState.lastAccess, "session.lastSendTime", session.lastSendTime) }, 15000) // 15 sec
Result: userData.lastAccess: 1494292505983 sessionState.lastAccess: 1494292505977 session.lastSendTime 1494292505977 lastAccessTime: 1494292505983 userData.lastAccess: 1494292519934 sessionState.lastAccess: 1494292505977 session.lastSendTime 1494292519933 lastAccessTime: 1494292519934
setTimeOut timeup: userData.lastAccess: 1494292505983 sessionState.lastAccess: 1494292505977 session.lastSendTime 1494292505977 lastAccessTime: 1494292519934 userData.lastAccess: 1494292519934 sessionState.lastAccess: 1494292519933 session.lastSendTime 1494292519933 lastAccessTime: 1494292519934
From: Nils Sent: Tuesday, May 9, 2:02 AM Subject: Re: [Microsoft/BotBuilder] [Question] session.userData.lastAccessTime seems not able to searlize inside the setTimeout function (#2716) To: Microsoft/BotBuilder Cc: wtam, Mention
Closed #2716https://github.com/Microsoft/BotBuilder/issues/2716.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/Microsoft/BotBuilder/issues/2716#event-1073254867, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AMTskJtKZ8uWCffZWcsLFBa58c5nulu0ks5r31hdgaJpZM4NTJME.
issue persists
You would need to call session.save() after modifying userData in your sample. Sending a message to the user does this for you but since you're not sending a message you need to explicitly save your state changes.
@Stevenic, thanks, done that before as I got its struggle over 1 week which I try every google search and the result are same as session.save() can't seems to save circular reference? As I say, the only way to make its work is define a global variable but it become global state not per session. So does it meant botframework currently not able to support session timeout.
@Stevenic, now I'm generating my own session id and use its as the Redis Key to store my own lastAccessTime value instead and its work. I think its may be a common ask and not sure if there'll be a feature to set the session timeout in future?
Oh I'm sorry I just re-read your original post. That's not going to work. The problem is you get a brand new session object in for every new message so when a second message for a user comes in it essentially in validates the session your timer is holding on to.
Using a timer like that isn't a great idea anyway because your node could re-boot and take your timers down with it. The better way to do this is something like this:
bot.use({
botbuilder: function (session, next) {
if (session.privateConversationData.previousAccess) {
var delta = new Date().time() - session.privateConversationData.previousAccess;
if (delta > 30000) {
session.clearDialogStack();
}
}
session.privateConversationData.previousAccess = session.sessionSate.lastAccess;
next();
}
});
This just evaluates each message as it comes in and evaluates if it's been too long since the users last reply. No need for a timer and it works even if your compute nodes get rebooted.
Thanks Steve but I do need to proactive close the session if its been idle say 15 mins instead of waiting the use to interact next time. So I do need a timer. So far the Redis Cache work for me so I'll keep it . Perhaps we may close this issue but if someone else in future asking the same for idle session timeout, perhaps is a feature request?
And just FYI when I use the node-redis, I've no issue with the restify server together on my PC (both running on the local host) but when I run it on Azure webApp, the pipeline seems got all mess up with http://undefined/undefined and iisnode permission etc. Till I move the Redis to another host not the local host. So Seems Azure web app do have some limitation on running multiple HTTP server on the same host as my next task is to figure out how to get the same SkypeCall connector with the Chat bot connector running in the same nodejs app as its has 2 different restify server and path
Please ignore my comment above restify server and http://undefined etc.
I will raise a new question in a separate thread on how to get SkypeCall Bot with LUIS intent as I couldn't find the same any IntentDialog (in ChatBot botbuilder) from the botbuilder-calling/lib/botbuilder.d.ts
I had the same problem, after going through the functionalities of bot-builder I found the solution. Use the bot.loadSession function which takes in the address of the session variable, this will help you access the latest session variable. Your code will look something like this :
bot.loadSession(address,(err,tsession)=>{
console.log("Inside load session",tsession.conversationData);
});
The address can be gained from the current session variable as : session.message.address Kudos