electron/docs/api/message-channel-main.md
Erick Zhao 18a76c6b9d
docs: uniformize module API doc format (#28920)
This PR ensures that all API modules are present in the README doc,
as there were a couple missing. It also formats all modules to contain
a level-1 heading and a blockquote description.
2021-04-29 09:56:31 +02:00

1.3 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 { MessageChannelMain } = require('electron')
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].on('message', (messageEvent) => {
    console.log(messageEvent.data)
  })
})

Instance Properties

channel.port1

A MessagePortMain property.

channel.port2

A MessagePortMain property.