# Understanding perspective session events

**URL:** <https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328>\
**Category:** Ignition Early Access\
**Created:** [March 14, 2019, 11:11am UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328 "2019-03-14T11:11:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![waldner](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@waldner](https://forum.inductiveautomation.com/u/waldner)\
**Post date:** [March 14, 2019, 11:11am UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/1 "2019-03-14T11:11:52Z")

</div>

As suggested in another thread, I’m trying to use the message passing mechanism to communicate from a perspective client to the gateway.

On the client, I’ve set up the following startup and shutdown event scripts:

`# startup`  
`system.util.sendMessage(project = 'myproject', messageHandler = 'start_session', payload = { "session_id": session.props.id, "device_id": session.props.device.identifier})`

`# shutdown`  
`system.util.sendMessage(project = 'myproject', messageHandler = 'end_session', payload = { "session_id": session.props.id, "device_id": session.props.device.identifier})`

On the gateway, I’m just logging that the message is being received:

`# start_session handler`  
`log = system.util.logger("Gateway Script Logger")`  
`log.info("START SESSION, ID: %s, DEVICE ID: %s" % (payload["session_id"], payload["device_id"]))`

`# end_session handler`  
`log = system.util.logger("Gateway Script Logger")`  
`log.info("END SESSION, ID: %s, DEVICE ID: %s" % (payload["session_id"], payload["device_id"]))`

Now, I launch the perspective session in the browser from designer, and in the logs I see:

`Socket connected to session. pageId=2471ea92`  
`START SESSION, ID: 0aed7b35-3fd4-40bd-b360-5719c8a9b552, DEVICE ID:`

(not sure about the empty device id). I close the browser, and in the logs I do NOT see the end session event, instead I only see:

`WebSocket disconnected from session.`

If I relaunch the session, again I do NOT see the start session event, but just:

`Socket connected to session. pageId=1e672231`

and the same if I continue to launche/close the session. Only after some time (presumably due to timeout) do I see the end session event in the log:

`END SESSION, ID: 0aed7b35-3fd4-40bd-b360-5719c8a9b552, DEVICE ID: browser-cd3e7e56-e24e-40a8-9358-bdf7e508071c`

(not sure why the device id now appears). Is this expected behavior? I was thinking that I would see a “start\_session” event each time I launch a session.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 14, 2019, 12:16pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/2 "2019-03-14T12:16:32Z")

</div>

The behavior you are seeing is what I would expect. HTTP is a stateless protocol, so state has to be layered on top of it (session via cookie). Disconnecting, by itself, doesn’t throw away the browser cookie, and shouldn’t, or minor network hiccups will log your users out. Which means the gateway has to _ **infer** _ an end of session with a timeout.

Keep in mind that your “client” scripts in Perspective are still running in the gateway.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 14, 2019, 2:48pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/3 "2019-03-14T14:48:02Z")

</div>

Closing a browser window doesn’t kill your session - it remains alive for 120 (maybe 60?) seconds by default, which can be configured here, in the Session Timeout section:

 ![03%20AM](https://us1.discourse-cdn.com/inductiveautomation/original/2X/6/612fc70dc32e351c4f5e42f5d3369201c72444a5.png)

So you won’t see the “end\_session” message sent from the Perspective Session for at least one minute, maybe two.

I’ll look into the device\_id being empty, but I just tested it myself and it worked as expected…

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 14, 2019, 2:50pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/4 "2019-03-14T14:50:50Z")

</div>

Oh, if you’re testing from the Designer, you won’t see a device\_id value - that only gets populated from an actual browser/application. I’ll see if there are any other instances which would cause it to not populate. Which browser were you using?

---

<div class="post-metadata">

**Author:** ![jspecht](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jspecht/32/6465_2.png) [@jspecht](https://forum.inductiveautomation.com/u/jspecht)\
**Post date:** [March 14, 2019, 3:12pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/5 "2019-03-14T15:12:19Z")

</div>

It’s also important to understand that each perspective session is tied to a specific project and if you have multiple tabs open in the same browser pointing to the same project, the session timeout will not start until the last tab for that project session disconnects. If a new tab opens and connects for that project session before the timeout elapses, the timeout is cancelled and the session lives on. Each new tab will attach to the existing project session on the gateway if it exists or if there is no project session yet (because it is the first tab open in your browser for a given project), one will be created.

For more details, see: [https://docs.inductiveautomation.com/display/DOC80/Perspective+Sessions](https://docs.inductiveautomation.com/display/DOC80/Perspective+Sessions)

---

<div class="post-metadata">

**Author:** ![waldner](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@waldner](https://forum.inductiveautomation.com/u/waldner)\
**Post date:** [March 14, 2019, 4:46pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/6 "2019-03-14T16:46:14Z")

</div>

> [@pturmel](#):
>
> Disconnecting, by itself, doesn’t throw away the browser cookie, and shouldn’t, or minor network hiccups will log your users out. Which means the gateway has to _ **infer** _ an end of session with a timeout.

Would an explicit call to system.perspective.logout() help? If so, I could tell users to quit by pressing a button that calls that.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 14, 2019, 4:47pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/7 "2019-03-14T16:47:28Z")

</div>

I wouldn’t expect so because the session is still open, you would just no longer be authenticated.

---

<div class="post-metadata">

**Author:** ![waldner](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@waldner](https://forum.inductiveautomation.com/u/waldner)\
**Post date:** [March 14, 2019, 5:31pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/8 "2019-03-14T17:31:01Z")

</div>

Firefox. It still looks strange to me, though, since the end\_session event _did_ receive a device\_id; where was that hiding when the session started?

Also, using the app on a mobile phone (tested with two so far) shows the same behavior. Phones are a koobee (can’t look up the model now) and a huawei vns-l31.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 14, 2019, 5:44pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/9 "2019-03-14T17:44:47Z")

</div>

It looks like the session startUp script is firing during the true initialization event, before the device ID is part of the Session… I’ll look into it.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [April 2, 2019, 6:55pm UTC](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/10 "2019-04-02T18:55:20Z")

</div>

The fix for this has been completed, but it was not done in time for the 8.0.0 release. Use of session.device.identifier in Session Startup scripts will continue to result in empty String values until the release of 8.0.1.
