Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Main and Renderer Processes

Electron Process Model, WebContents and Utility Processes

See the full Electron process model: main, renderers, GPU and utility processes, webContents and its events, worker threads, and when to move heavy work out of the main process.

You now know the two main roles, the main process and the renderer process. Behind them sits Chromium's full multi-process architecture, and Electron adds a few objects that tie everything together. This lesson draws the complete picture, introduces webContents (the object that represents a renderer from the main process), and shows the options for running heavy work without freezing your app.

Why multiple processes?

Chromium splits a browser into processes for two reasons: stability (a crash in one tab does not kill the others) and security (a compromised page runs with fewer privileges and can be isolated). Electron inherits the model, so an Electron app is really a small group of operating system processes. You can see them in Task Manager or Activity Monitor.

The processes of one Electron app

Main process One. Node.js. Owns windows.
Renderer processes One per window or frame
GPU process Draws and composites pixels
Utility processes Network, audio, your own tasks

You write main and renderers; Electron manages the rest.

1 / 2

webContents: the main process's handle on a page

Every window has a webContents object. It is an EventEmitter that lives in the main process and represents the web page inside the window. You reach it with win.webContents. It is how the main process loads URLs, sends messages, reads the page's state, and reacts to what happens inside it.

JavaScript
const win = new BrowserWindow({ show: false })
const wc = win.webContents

wc.on('did-finish-load', () => console.log('page loaded'))
wc.on('page-title-updated', (event, title) => console.log('title:', title))
wc.on('console-message', (event, level, message) => console.log('renderer says:', message))
wc.on('render-process-gone', (event, details) => console.log('renderer crashed:', details.reason))

wc.loadFile('index.html')
wc.send('settings-changed', { theme: 'dark' })   // message to the renderer (see IPC chapter)
wc.setZoomFactor(1.25)
wc.executeJavaScript('document.title')           // run code in the page; use sparingly

webContents also powers printing (print(), printToPDF()), screenshots (capturePage()), find-in-page, devtools and navigation (goBack(), reload()). Windows are not the only owners: a WebContentsView can place several web contents inside one window, and <webview> or <iframe> elements create nested contents. Each frame has an id and a webFrameMain handle if you need to talk to a specific frame.

Handle render-process-gone in real apps: a renderer can crash (out of memory, a native bug). Reloading the window or showing an error page is much friendlier than leaving a dead window.

Seeing the relationship

This stand-in models the relationship: a window owns a webContents emitter, the main process subscribes to its events, and the "renderer" can crash independently while the main process stays alive.

Node.js Stand-in: webContents events and a renderer crash

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Moving heavy work out of the main process

Imagine you need to hash a large file, parse a 100 MB log, or compress images. If you do that in the main process, every window stutters. Choose the right tool:

Tool Runs in Can use Electron APIs? Use when
Web Worker A renderer No (web APIs only) CPU work that serves one page
worker_threads Main process, another thread No Electron UI APIs Compute that must stay in the main process
utilityProcess.fork() A separate Node.js child process Message ports only Isolated, crash-safe or untrusted-ish work
Hidden window A renderer Through preload Rare; prefer the above

utilityProcess is Electron's own way to start a Node.js process that can crash independently and communicate with the main process through message channels:

JavaScript
const { utilityProcess, app } = require('electron')
const path = require('node:path')

app.whenReady().then(() => {
  const child = utilityProcess.fork(path.join(__dirname, 'worker.js'))
  child.on('message', (result) => console.log('result:', result))
  child.postMessage({ task: 'sum', numbers: [1, 2, 3] })
})
JavaScript
// worker.js (runs in the utility process)
process.parentPort.on('message', ({ data }) => {
  if (data.task === 'sum') {
    process.parentPort.postMessage(data.numbers.reduce((a, b) => a + b, 0))
  }
})

The same request/response shape works with Node's own worker_threads, which you can run here. This stand-in sends a task to a worker thread and shows that the main thread keeps ticking while the worker computes.

Node.js Offload a CPU task to a worker thread

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Process IDs and memory

process.getProcessMemoryInfo() and app.getAppMetrics() report per-process CPU and memory, which helps when you tune performance later. webContents.getOSProcessId() gives the operating system's PID for a renderer if you want to find it in Task Manager.

Site isolation and frames

Chromium puts pages from different sites into different renderer processes when it can. Electron does not turn on every aspect of this for file:// content, so do not rely on it as a security boundary; use sandboxing, context isolation and careful navigation rules instead (see the security chapter).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is webContents?

    webContents is an EventEmitter in the main process that loads URLs, sends messages and reports events for a page.
  2. 2. Which event should you handle to recover from a renderer crash?

    render-process-gone reports crashes so you can reload or show an error page.
  3. 3. Which option runs isolated Node.js code in a separate process that can crash without taking the main process down?

    A utility process is a separate Node.js child process that talks to the main process over message ports.
  4. 4. Why not do heavy computation directly in the main process?

    The main event loop handles every window's events; blocking it freezes the app.

Summary

  • An Electron app is several OS processes: main, renderers, GPU and utility processes.
  • webContents is the main process's handle on a page: load, send, inspect, react to events.
  • Handle render-process-gone to recover from renderer crashes.
  • Offload heavy work to Web Workers, worker_threads or utilityProcess.
  • Do not treat process separation alone as your security boundary.

Next: Loading Content, Navigation and Protocols