Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Electron Core Concepts

Electron Core Concepts

Electron is essentially a multi-process desktop application framework that combines Chromium (rendering layer) and Node.js (system layer), implementing UI display, logic control, and secure communication through an independent process model.

Below is the core Electron architecture, showing the main process, renderer process, IPC communication, and the Preload relationship.

Main process (A):

** Renderer process (B):**

** Preload script (B2):**

IPC communication (dashed arrows):

Operating system (C):


Main Process

The main process is the "command center" of an Electron app, running in the Node.js environment.
It is responsible for creating windows, managing the app lifecycle, calling system APIs, and communicating with the renderer process.
After the whole app starts, the system first runs the main process script (usually main.js).

Responsibilities of the main process

Main process lifecycle

The main process lifecycle matches the application lifecycle, starting from the app module:

Example

const

{

app

,

BrowserWindow

}

=

require

(

'electron'

)

;

app.

whenReady

(

)

.

then

(

(

)

=>

{

 

const

win

=

new

BrowserWindow

(

{

width

:

800

,

height

:

600

}

)

;

  win.

loadFile

(

'index.html'

)

;

}

)

;

app.

on

(

'window-all-closed'

,

(

)

=>

{

 

if

(

process.

platform

!==

'darwin'

)

app.

quit

(

)

;

}

)

;

app module details

app The module is used to control the entire application lifecycle:

BrowserWindow window management

The main process passes BrowserWindow to create a visible window:

Example

const

win

=

new

BrowserWindow

(

{

  width

:

1024

,

  height

:

768

,

  resizable

:

true

,

  webPreferences

:

{

    preload

:

'./preload.js'

,

    nodeIntegration

:

false

 

}

}

)

;

- `preload`: preload script - `nodeIntegration`: whether to allow the renderer process to use Node.js

Native features such as menus, tray, and dialogs

Electron provides multiple modules to access system features:

dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] });


Renderer Process

The renderer process is responsible for displaying the interface, running in the Chromium environment.
Each BrowserWindow has an independent renderer process.

Characteristics of the renderer process

Multiple windows and multiple renderer processes

Each Electron window (BrowserWindow) starts an independent renderer process:
Closing one window does not affect the others, similar to the browser's multi-tab model.
This makes the app safer and more reliable in crash recovery.

Loading and displaying web pages

The content of the renderer process is loaded by the main process:

win.loadFile('index.html');    // 加载本地页面

// 或者

win.loadURL('https://example.com'); // 加载远程网页

Restrictions in the renderer process


Inter-Process Communication (IPC)

One of Electron's core mechanisms is communication between the main process and the renderer process, called IPC (Inter-Process Communication).

ipcMain and ipcRenderer

One-way communication example:

Example

// main.js

const

{

ipcMain

}

=

require

(

'electron'

)

;

ipcMain.

on

(

'ping'

,

(

event

,

arg

)

=>

{

  console.

log

(

arg

)

;

// outputs "hello from renderer"

}

)

;

// renderer.js

const

{

ipcRenderer

}

=

require

(

'electron'

)

;

ipcRenderer.

send

(

'ping'

,

'hello from renderer'

)

;

Two-way communication example (main process returns data):

ipcMain.on('getVersion', (event) => {

  event.reply('getVersionResponse', app.getVersion());

});

Received on the renderer side:

ipcRenderer.on('getVersionResponse', (event, version) => {

  console.log(version);

});

invoke/handle pattern (recommended approach)

Electron provides a simpler asynchronous request method:

Main process:

ipcMain.handle('get-app-path', () => app.getPath('userData'));

Renderer process:

const result = await ipcRenderer.invoke('get-app-path');

console.log(result);

This approach is similar to fetch requests, avoiding multiple layers of callbacks.

Best practices for message passing


Preload Scripts

Preload scripts run before the renderer process is created, between the main process and the web page.
Their role is to safely "bridge" Node.js functionality to the renderer page.

The role and significance of preload

// preload.js

const { contextBridge, ipcRenderer } = require('electron');



contextBridge.exposeInMainWorld('electronAPI', {

  getAppVersion: () => ipcRenderer.invoke('getVersion')

});

Used by the renderer process:

// index.html



contextBridge secure communication

contextBridge is a secure API provided by Electron, used to:

Usage in sandbox mode

If the sandbox is enabled (sandbox: true), the renderer process cannot use Node.js at all.
At this point preload is the only bridge, responsible for:


Summary

module Purpose Runtime environment
Main process (Main) Controls the app lifecycle, windows, system interfaces Node.js
Renderer process (Renderer) Displays the UI, executes frontend logic Chromium
IPC Implements communication between the main and renderer processes Two-way pipeline
Preload Safely bridges Node and Web Middle layer

In one sentence:


The main process is like the "control room", the renderer process is like "staff at each window",
IPC is the "walkie-talkie", and Preload is the "filtered secure channel".