parentPort

Interface for communication with parent process.

Process: Utility

parentPort is an EventEmitter. This object is not exported from the 'electron' module. It is only available as a property of the process object in the Electron API.

  1. // Main process
  2. const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
  3. child.postMessage({ message: 'hello' })
  4. child.on('message', (data) => {
  5. console.log(data) // hello world!
  6. })
  7. // Child process
  8. process.parentPort.on('message', (e) => {
  9. process.parentPort.postMessage(`${e.data} world!`)
  10. })

Events

The parentPort object emits the following events:

Event: ‘message’

Returns:

  • messageEvent Object
    • data any
    • ports MessagePortMain[]

Emitted when the process receives a message. Messages received on this port will be queued up until a handler is registered for this event.

Methods

parentPort.postMessage(message)

  • message any

Sends a message from the process to its parent.