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:

  1. // Main process
  2. const { BrowserWindow, MessageChannelMain } = require('electron')
  3. const w = new BrowserWindow()
  4. const { port1, port2 } = new MessageChannelMain()
  5. w.webContents.postMessage('port', null, [port2])
  6. port1.postMessage({ some: 'message' })
  7. // Renderer process
  8. const { ipcRenderer } = require('electron')
  9. ipcRenderer.on('port', (e) => {
  10. // e.ports is a list of ports sent along with this message
  11. e.ports[0].onmessage = (messageEvent) => {
  12. console.log(messageEvent.data)
  13. }
  14. })

Instance Properties

channel.port1

A MessagePortMain property.

channel.port2

A MessagePortMain property.