Perspective Session Cleanup

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.

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:

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

Any reason a session shutdown event would not work?

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

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.