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

Electron Architecture

Electron Architecture

You can think of Electron as:

A multi-process application container with a built-in browser engine (Chromium) and a Node.js runtime,


Working together through inter-process communication (IPC) to build a desktop app of "web pages + system capabilities".

Its core components are:

Role Description Analogy
Main Process (main process) The brain of Electron, controlling the app lifecycle, creating windows, and calling system APIs Operating system manager
Renderer Process (renderer process) The web page environment (HTML, CSS, JS) run by each window Browser tab
Preload Script (preload script) Runs before rendering, can bridge main process APIs for the web page A secure "middle layer"
IPC (inter-process communication) The communication channel between the main process and the renderer process Telephone line
BrowserWindow object The "window container" created by the main process, loading web pages inside Browser window
App module Controls the app lifecycle (startup, exit) Master control

Main Process (Main Process)

The main process is the "brain" of an Electron app, and every Electron app has exactly one main process. It is responsible for:

Example

// main.js - main process example

const

{

app

,

BrowserWindow

}

=

require

(

'electron'

)

;

function

createWindow

(

)

{

 

// Create the browser window

 

const

mainWindow

=

new

BrowserWindow

(

{

    width

:

800

,

    height

:

600

,

    webPreferences

:

{

      nodeIntegration

:

true

,

      contextIsolation

:

false

   

}

 

}

)

;

 

// Load the application's index.html

  mainWindow.

loadFile

(

'index.html'

)

;

}

// Called when Electron has finished initialization

app.

whenReady

(

)

.

then

(

createWindow

)

;

Renderer Process

The renderer process is responsible for displaying the user interface; each Electron window is an independent renderer process:

Example

   

<p>My Electron App</p>

   

Hello Electron!

   

   

   

Preload Scripts

Preload scripts are the "bridge" connecting the main process and the renderer process:

Example

// preload.js - preload script example

const

{

contextBridge

,

ipcRenderer

}

=

require

(

'electron'

)

;

// Expose safe APIs to the renderer process

contextBridge.

exposeInMainWorld

(

'electronAPI'

,

{

  showDialog

:

(

message

)

=>

ipcRenderer.

invoke

(

'show-dialog'

,

message

)

}

)

;


Inter-Process Communication (IPC)

IPC communication patterns

IPC (Inter-Process Communication) is the communication core of Electron.

Because the main process and renderer process are independent, data must be passed through IPC.

Communication direction

Type Main process listens Renderer process sends
Renderer -> Main ipcMain.on(channel, handler) ipcRenderer.send(channel, data)
Main -> Renderer event.sender.send(channel, data) ipcRenderer.on(channel, callback)

Basic communication example

Main process code:

Example

const

{

ipcMain

,

dialog

}

=

require

(

'electron'

)

;

// Listen for messages from the renderer process

ipcMain.

handle

(

'show-dialog'

,

async

(

event

,

message

)

=>

{

 

const

result

=

await dialog.

showMessageBox

(

{

    type

:

'info'

,

    message

:

message

,

    buttons

:

[

'OK'

,

'Cancel'

]

 

}

)

;

 

return

result

;

}

)

;

Renderer process code:

Example

// Communicate through the API exposed by the preload script

document.

getElementById

(

'btn'

)

.

addEventListener

(

'click'

,

async

(

)

=>

{

 

const

result

=

await window.

electronAPI

.

showDialog

(

'Hello, Electron!'

)

;

  console.

log

(

'User clicked:'

,

result.

response

)

;

}

)

;


Architecture advantages and features

Advantage comparison

Feature Electron Traditional desktop development
Development technology Web technologies (HTML/CSS/JS) Native languages (C++/C#/Java)
Cross-platform support Develop once, run on multiple platforms Need to develop separately for each platform
Development efficiency High, leverages the existing Web ecosystem Lower, requires learning platform-specific technologies
Performance Relatively low, uses more resources High, native performance
Installer size Larger (includes Chromium) Smaller

Core features

  1. Cross-platform consistency: the app looks and behaves consistently across all operating systems
  2. Web ecosystem utilization: can directly use npm packages and web frameworks
  3. Rapid prototyping: based on familiar web technologies, short development cycle
  4. Rich API: provides a complete API set for accessing native system features

Practical Application Examples

Let us create a simple file manager app to demonstrate the Electron architecture:

Main process (main.js):

Example

const

{

app

,

BrowserWindow

,

ipcMain

,

dialog

}

=

require

(

'electron'

)

;

const

fs

=

require

(

'fs'

)

.

promises

;

const

path

=

require

(

'path'

)

;

let mainWindow

;

function

createWindow

(

)

{

  mainWindow

=

new

BrowserWindow

(

{

    width

:

1000

,

    height

:

700

,

    webPreferences

:

{

      preload

:

path.

join

(

__dirname

,

'preload.js'

)

,

      contextIsolation

:

true

   

}

 

}

)

;

  mainWindow.

loadFile

(

'index.html'

)

;

}

// Handle file reading

ipcMain.

handle

(

'read-file'

,

async

(

event

,

filePath

)

=>

{

 

try

{

   

const

content

=

await fs.

readFile

(

filePath

,

'utf-8'

)

;

   

return

{

success

:

true

,

content

}

;

 

}

catch

(

error

)

{

   

return

{

success

:

false

,

error

:

error.

message

}

;

 

}

}

)

;

// Handle file selection

ipcMain.

handle

(

'select-file'

,

async

(

)

=>

{

 

const

result

=

await dialog.

showOpenDialog

(

mainWindow

,

{

    properties

:

[

'openFile'

]

,

    filters

:

[

     

{

name

:

'Text file'

,

extensions

:

[

'txt'

,

'md'

,

'js'

,

'html'

,

'css'

]

}

   

]

 

}

)

;

 

 

return

result

;

}

)

;

app.

whenReady

(

)

.

then

(

createWindow

)

;

Preload script (preload.js):

Example

const

{

contextBridge

,

ipcRenderer

}

=

require

(

'electron'

)

;

contextBridge.

exposeInMainWorld

(

'fileAPI'

,

{

  readFile

:

(

filePath

)

=>

ipcRenderer.

invoke

(

'read-file'

,

filePath

)

,

  selectFile

:

(

)

=>

ipcRenderer.

invoke

(

'select-file'

)

}

)

;

Renderer process (index.html):

Example

   

<p>Simple File Manager</p>

   

   

       

Simple File Manager

       

       

       

       

           

File content:

           

Please select a file...

       

   

   


Other important modules (available in the main process)

Module name Purpose
app Controls the app lifecycle (startup/exit)
Menu / MenuItem Creates the application menu bar
Tray System tray icon
Notification System notifications
dialog Opens file/save dialogs
shell Opens files or URLs with the system default program
nativeImage Manipulates images (icons)
clipboard Manipulates clipboard contents
powerMonitor Listens for system power events
screen Gets screen information (multi-monitor support)

Security architecture and sandbox isolation

Because the renderer process can run web pages, to prevent malicious code from attacking the system, Electron promotes the following security designs:

Strategy Description
Disable NodeIntegration Default false, to prevent web pages from directly calling system APIs.
Enable ContextIsolation Lets page scripts and Preload APIs run in separate contexts.
Preload + contextBridge Explicitly control the safe APIs exposed to the frontend.
Content Security Policy (CSP) Restrict script sources to prevent XSS.
sandbox mode Sandbox can be enabled to fully isolate the renderer process.
Validate remote URLs Untrusted remote content must be whitelisted.

Runtime flow (from startup to rendering)



1、启动应用 (electron .)

     ↓

2、执行 main.js(主进程启动)

     ↓

3、app.whenReady() 触发,创建 BrowserWindow

     ↓

4、BrowserWindow 启动新的渲染进程(Chromium 实例)

     ↓

5、preload.js 先执行(在隔离上下文中)

     ↓

6、index.html 加载并显示(运行前端框架)

     ↓

7、渲染进程通过 IPC 调用主进程逻辑

     ↓

8、主进程处理请求、返回结果

     ↓

9、用户关闭窗口 → 主进程监听 → app.quit()


Master-slave multi-window model (multi-process parallelism)

Diagram:



主进程 (Main)

 ├── 窗口1 → 渲染进程A (index.html)

 ├── 窗口2 → 渲染进程B (settings.html)

 └── 窗口3 → 渲染进程C (dashboard.html)


Electron internal core components (underlying dependencies)

Level Technology
UI rendering layer Chromium (Blink + V8)
System interface layer Node.js (libuv + C++ bindings)
Process management layer Electron core (C++ + JavaScript)
Application logic layer Your JS / TS code (Main + Renderer)

The JS code you write runs in the Node environment wrapped by Electron, while the rendering UI runs in the Chromium WebView.