Electron Part 2 - Process Communication

foreword

During development with Electron, rendering process development becomes simple. Most styling and business logic can be implemented using familiar front-end technologies. But inevitably, sometimes, the rendering process needs to communicate with the main process. Today, let's talk about the main process and the rendering process.

What is the main process Main Process

Typically, scripts running in the main process control the life cycle of the application, display the graphical user interface and its elements, perform native operating system interactions, and create rendering processes in web pages. Electron applications can only have one main process.

Simply put, the main process controls the carrier of the entire application and all rendering processes. If you want to perform functions such as closing, minimizing, and topping the window, you need to operate in the main process.

What is the Renderer Process

The rendering process is our application window. Multiple browser windows can be created, each using its own independent rendering process. The rendering process is where we usually do front-end development.

The main process communicates with the rendering process

The modules in Electron are divided into three types of Main Process modules, Renderer Process modules and modules available for both processes.

event listener

The module method in the main thread cannot be called directly by the rendering process. Therefore, the most common method is to monitor events through the ipcMain module in the main process.

Events are sent in the rendering process through the send method in ipcRenderer

In this way, the rendering process calls the method in the main process.

module call

There is another situation, that is, we can't handle communication only by event monitoring, we want to get the main process module operation directly in the rendering process. Say a scene, that is cookie.

When developing applications with Electron, js-cookie can no longer be used for cookie operations. There is a module called session in the main process, and cookies can be manipulated through the session module.

Make an http request in the rendering process, and want to get, set, and remove the cookie. Do I still need to write a few event listeners in the main process and a few event listeners for the rendering process to call back and forth? That's definitely anti-human.

Fortunately, there is a remote module in Electron. The main process module can be used in the rendering process. This provides an easy way for the renderer process to communicate with the main process. Before using the remote module, you need to set enableRemoteModule in webPreferences to true to enable the remote module.

Then, we can pass

let session = require('electron').remote.session;

Get the session module. How the specific cookie operates and what fields the cookie object has can be found in the official website documentation.

Summarize

The above describes how the rendering process calls the main process. The main process can also call the rendering process. In the same way, how to operate can be found on the official website. ps It is best to check the documentation on the official website. Other documents on the Internet may not know which version they are. A victim who read Chinese documents on the Internet at the time and did not know when the callback was changed to a promise said this.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135963&siteId=291194637