# Perspective Session Cleanup

**URL:** <https://forum.inductiveautomation.com/t/perspective-session-cleanup/87402>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 15, 2024, 1:47pm UTC](https://forum.inductiveautomation.com/t/perspective-session-cleanup/87402 "2024-03-15T13:47:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Kuss](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Matthew\_Kuss](https://forum.inductiveautomation.com/u/Matthew_Kuss)\
**Post date:** [March 15, 2024, 1:47pm UTC](https://forum.inductiveautomation.com/t/perspective-session-cleanup/87402/1 "2024-03-15T13:47:43Z")

</div>

I have some cleanup code that runs when a perspective user logs out (via a log-out button), but this code doesn't execute on a session closes unexpectedly. I'd like to instead run this code on the gateway when a session ends or is lost. Is there an easy way to trigger this script?

My initial though is to setup a session message and let the gateway run the code if it misses X number of messages from a given session. That seems overly complex for what I'm trying to do, so I'm hoping that there is an easier event driven in way that I don't know about.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [March 15, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/perspective-session-cleanup/87402/2 "2024-03-15T14:02:00Z")

</div>

Perspective session messages are sent from the gateway (any script that could send one has to run on the gateway in Perspective) so it wouldn't work as a heartbeat to detect browser connection loss.

The session shutdown event does eventually detect disconnection, but it takes time:

> [@Understanding perspective session events](https://forum.inductiveautomation.com/t/understanding-perspective-session-events/23328/3):
>
> 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: 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…

More discussion on the topic, including a link to a relevant feature request:

> [@Detecting lost connection to the gateway](https://forum.inductiveautomation.com/t/detecting-lost-connection-to-the-gateway/78502):
>
> We are testing how perspective clients react to a communication loss to the gateway due to a cable break or the gateway loosing power. We observe that it takes 2,5 minutes before the client displays the "No Connection to Gateway" banner. [image] Looking into the browser console, we see that after approximately 2 minutes and 15 seconds we get the message store.Channel: Disconnecting websocket with message: "Idle time reached." After 15 more seconds we get the message store.Channel: Websocket c…

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [March 15, 2024, 2:14pm UTC](https://forum.inductiveautomation.com/t/perspective-session-cleanup/87402/3 "2024-03-15T14:14:01Z")

</div>

Any reason a session shutdown event would not work?

> **[Perspective Session Event Scripts | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/ignition-modules/perspective/scripting-in-perspective/perspective-session-events-scripts#startup-and-shutdown-event-scripts)**
>
> Perspective offers a collection of Session Events designed to allow the Gateway to track and interact with the Session at critical moments. Specifically, they are scripts that run in the Gateway when a Session starts up, shuts down, or runs a Native...

It would not detect the browser closing right away (rather after the session timeout), but it **would run** once the session properly ends.

---

<div class="post-metadata">

**Author:** ![Matthew\_Kuss](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Matthew\_Kuss](https://forum.inductiveautomation.com/u/Matthew_Kuss)\
**Post date:** [March 15, 2024, 5:46pm UTC](https://forum.inductiveautomation.com/t/perspective-session-cleanup/87402/4 "2024-03-15T17:46:33Z")

</div>

Thank you for the advice. I'll try the Session Event and see if that fixes my problem. I forgot it runs on the Gateway in perspective therefore it should still run.
