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

Instance Properties

channel.port1

A MessagePortMain property.

channel.port2

A MessagePortMain property.