electron/docs/api/message-channel-main.md
trop[bot] e90404be7d
docs: no class inheritance (#47433)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Erick Zhao <ezhao@slack-corp.com>
2025-06-10 13:12:24 +02:00

1.5 KiB

MessageChannelMain

MessageChannelMain is the main-process-side equivalent of the DOM MessageChannel object. Its singular function is to create a pair of connected MessagePortMain objects.

See the Channel Messaging API documentation for more information on using channel messaging.

Class: MessageChannelMain

Channel interface for channel messaging in the main process.

Process: Main

Example:

// Main process
const { BrowserWindow, MessageChannelMain } = require('electron')
const w = new BrowserWindow()
const { port1, port2 } = new MessageChannelMain()
w.webContents.postMessage('port', null, [port2])
port1.postMessage({ some: 'message' })

// Renderer process
const { ipcRenderer } = require('electron')
ipcRenderer.on('port', (e) => {
  // e.ports is a list of ports sent along with this message
  e.ports[0].onmessage = (messageEvent) => {
    console.log(messageEvent.data)
  }
})

Warning

Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.

Instance Properties

channel.port1

A MessagePortMain property.

channel.port2

A MessagePortMain property.