Lifecycle Events

The Lifecycle object emits a number of events that your app can respond to. See the Lifecycle class documentation for complete details.

State change

The Lifecycle class has four states:

  • Foreground
  • In Transition to Background
  • Background
  • In Transition to Foreground

Your app can listen for an onstatechange event like this:

senza.lifecycle.addEventListener("onstatechange", (event) => {
    console.log("Lifecycle state:", event.state);
});

See the Stopwatch tutorial for an example that shows how to listen for lifecycle state changes.

User inactivity

To conserve server resources, Senza includes an automatic sleep feature that activates when the device hasn’t been used for an extended period. They may have stopped watching, but they may also just be watching a long movie.

Senza tracks user activity and, by default, will attempt to disconnect the session after three hours of inactivity. Before doing so, it sends a message to your app that asks, "Are you still there?" If the user responds by pressing any button, the inactivity timer resets. If they don’t, the session will disconnect after the specified countdown.

senza.lifecycle.addEventListener("userinactivity", (e) => {
    showMessage(`Are you still there? Press any button in the next ${e.timeout} seconds to keep watching.`);
});

To test this feature in your app without waiting the full three hours, you can manually simulate an inactivity warning by dispatching a fake event.

senza.lifecycle.dispatchEvent(new CustomEvent("userinactivity", {detail: {timeout: 30}}));

User disconnected

When a user session ends—either due to prolonged inactivity, switching away from the app, or explicitly exiting—the Senza platform triggers a userdisconnected event. This event signals that the current session is no longer active and that your app should begin cleaning up any associated resources.

Handling this event allows your application to indicate to its backend server that the client has been powered off. This is unique to Senza in the sense that the application can keep running in the browser in the cloud even after the device has been powered off. This may be useful to close a session or for analytics purposes.

senza.lifecycle.addEventListener("userdisconnected", () => {
    console.log("User disconnected.");
});