How Electron app can listen (handle) windows log off and shutdown events?


I’m developing an electron app that contains a webview to wrap a web application. This app must be able to send a post request with some data to the application server each time the user shutdown or log off from windows. How electron or node.js can listen to those windows events? (A windows service is not an option in my case).

I’ve been trying this for a while now, but I could not accomplish and have been looking in everywhere without lucky.

Thanks in advance.


I suspect that a Windows service is the only option.


Thanks for your answer.

I’m sorry I’m new on Electron/node js. Could you clarify why a service is the only option?

How does electron handle these events right now?


Because I skimmed the Node docs and didn’t see anything about listening for system events, and I typed a couple of searches into NPM and didn’t find anything. Does Windows send a different code to applications that are being closed because it’s shutting down? Do applications receive notice that Windows is logging out? If not, then you need a separate program running at the service level to watch for that.


For logoff events, Windows send WM_WTSSESSION_CHANGE message to handlers that are registered to receive these events.I thought that nodejs was registered to receive this messages through some type of SIGNAL from the process module. So maybe nodejs is not registered to receive these messages? I may need to find a way to register the app handler window to receive this massage?


Your tone indicates that you haven’t found a source of authoritative information on this. That would be the first step.


OK so if you haven’t implemented this yet. I found something that was interesting. Ever used the edge module?

Apparently, it acts as a interop between C# and nodeJS. So you can create C# code as a comment in nodeJS and actually have it interact with the .NET framework. Theoretically, maybe you could use C# to gain access to the windows events and then tie that to a callback in nodeJS to determine when something like a logoff or system shutdown is occurring.

Just a thought.


I didn’t have time to implemented this yet. I took a look on the Edge.js, sounds promising. Thanks for the replay.


No problem, good luck!