Learn how to disable the suspension mode of the OS within your Electron Application.

How to prevent that the system enters in suspension (sleep) mode in Electron Framework

Some applications require that the Sleep Mode or Screen Sleep Mode is disabled as this would change the behaviour of the app. Applications like Video Players, Download Managers and others suffer when such a feature is enabled. As usual, the user won't disable the sleep mode of the computer just because your app requires it (well, not always), that's why the application should look for a way to workaround this Sleep Mode without making that the user does something special.

That's why the Power Save Blocker API of Electron was introduced in the recent version and in this article, we'll show you how to use it easily inside your Electron based hybrid desktop application.

Important

Your Electron Framework version needs to be at least v1.7.5 in order to work properly with the Power Save Blocker API.

How the API works

The Power Save Blocker API of electron block the system from entering low-power (sleep) mode:

const {powerSaveBlocker} = require('electron')

The module offers only 3 methods:

start

The start method expects as first argument a string with the type of blocker that you want to use:

  • prevent-app-suspensionPrevent the application from being suspended. Keeps system active but allows screen to be turned off. Example use cases: downloading a file or playing audio.
  • prevent-display-sleep: Prevent the display from going to sleep. Keeps system and screen active.

The execution of the start method returns an ID that can be used either to verify if the service is running or just to stop it.

stop

The method expects as first argument the ID of the service started by the start method:

  • idThe power save blocker id returned by powerSaveBlocker.start.

isStarted

The method verifies if the blocker service has been correctly started:

  • id Integer - The power save blocker id returned by powerSaveBlocker.start.

Using the API

The following code shows how to prevent the screen from sleeping using the API:

Important

The code needs to be executed in the renderer process (index.html or JS files imported on it), not in the main, otherwise the blocker won't work.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Electron Application</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0, maximum-scale=1, minimum-scale=1">
    </head>
    <body>
        <div id="app"></div>

        <script>
            // Require remote of electron
            const remote = require("electron").remote;

            // Use the 'prevent-display-sleep' mode
            const id = remote.powerSaveBlocker.start("prevent-display-sleep");

            // If needed, stop the blocker
            // remote.powerSaveBlocker.stop(id);
            
            console.log("Power Save Blocker Started: ", powerSaveBlocker.isStarted(id));
        </script>
    </body>
</html>

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors