How to Detect Session Startup and Shutdown in Ignition Perspective

Dear all,
I would like to identify when a Perspective session (web page or app) starts up and shuts down.
My goal is to write to a tag when the session starts and when it ends.
I’ve tried using the session startup and shutdown events, but my script doesn’t seem to work.

Has anyone successfully used session startup or shutdown events in Perspective?
Any suggestions or examples would be greatly appreciated!

What happened exactly ? Got any errors in the logs ?
Show us the scripts you used.

Nope, i don't see the logfile. I try to write a memory tag to control the events:

Add some logging to both events and make sure you see a corresponding entry in the logs (go to your gateway's webpage, and find the log page. It's in the "status" section).

Is there a script instruction to identify and use the idsession?

I'm not sure what you mean. You have direct access to session properties in the session events.

logger = system.util.getLogger("SessionStartup")
logger.infof("started session %s", session.props.id)

image

ok your instruction works but i would like to use the relevant id session to write a tag

You're gonna have to be more explicit than that.

Actually, what are you trying to solve ? What's the end goal ?

Apologies for the confusion — my end goal is to write to a specific tag when a particular session is closed. I tried several if instructions but the ones I really need don't work

Are you sure the session had closed when you checked? Have a look in the status page of the Web portal at the perspective sessions running. A session does not end simply when you close the browser tab running a client. After that it waits for the session timeout before the session running in the gateway shuts down

2 Likes

Yes, you are right but I'm sure of the closed session, i see the log view and there are all the session, and when the script doesn't work.

Nick's point is that closing a browser tab, or closing the browser entirely, does not end the session. Browsers don't tell webservers when they close, so the gateway cannot close the session immediately. It has to time out.

Your desire to act on session close is misguided/impractical. Find another way. (Share why you think you need this.)

Your script has a syntax error. Case-sensitive variable name, it seems.

I agree. For me is ok that the script running when the timeout expires.

s['userAgent'] is not where you check for whether the session is a designer or not. You would checks['sessionScope'] instead.

Don't post images of scripts, post the script itself, and format it with the </> button.

Key Description Value
userAgent Details about the device or browser running the Session. String
id The Session ID (or Designer ID if called from the Designer). String
username The logged-in user's name or Unauthenticated if the Session is not authenticated. String
project The name of the project active in the Session. String
uptime The time (in milliseconds) the Session has been active since creation. Integer
clientAddress The IP address or hostname of the client running the Session. String
lastComm The time (in milliseconds) since the last communication with the Gateway. Integer
sessionScope The environment where the Session is running: Designer, Browser, iOS, or Android. String
activePages The number of pages currently active in the Session. Integer
recentBytesSent The number of bytes sent to the Gateway in teh most recent communication. Integer
totalBytesSent The total number of bytes sent to the Gateway by the Session. Integer
pageIds A list of the IDs of pages currently open in the Session. List
authorized Whether the user was authorized when the Session data was retrieved (True or False) Boolean

Are you talking about the "ProjectFilter" arg name? I can't find anything else :thinking:

I think he is talking about sessionID vs sessionId, it's commented out.

1 Like

To get that logged error, a similar usage must have been present.

Right, and it is commented as this line doesn't work. I was just pointing Nick where to look.

ok i correct the second one sessionId, but the if instruction doesn't work

now my test tag values 200. but if it works well would value 1000

To answer why your script isn't working though: when the shutdown event fires, the session is already gone, so getSessionInfo doesn't contain the session anymore

Going back a step though, with the actual code you've got, it seems superfluous to be looping through the sessions at all, when at the end of it all it's doing is writing the session id to a tag. Can't you just skip the loop and just write the session.props.id to the tag? Unless of course there's some other logic you want to do that you haven't shown.