Class: IPCResponder

IPCResponder

IPCResponder is a base class for implementing communication partners which use Electrons Inter Process Communication (IPC) facilities.

With IPCResponder#registerTopic you can register a topic which can be called by the other peer. Vica versa, IPCResponder#ask and IPCResponder#tell allow to call upon such topics.

Constructor

new IPCResponder(send, on)

Creates a new IPCResponder and sets up the communication stack.
Parameters:
Name Type Description
send function A function that allows sending an event via the IPC infrastructure
on function A function that allows setting up a listener on the IPC infrastructure
Source:
Examples

Create IPCResponder for Host Process

const electron = require('electron')
let mainWindow = new electron.BrowserWindow()
const ipcMain = electron.ipcMain
const webContents = mainWindow.webContents

class HostIPCResponder extends IPCResponder {
  constructor() {
    super(webContents.send.bind(webContents), ipcMain.on.bind(ipcMain))

    this.registerTopic('hello', (payload) => Promise.resolve({
      text: 'hello ' + payload.name + ' too'
    })
  }
}

Create IPCResponder for Renderer Process

const ipcRenderer = window.require('electron').ipcRenderer

class RendererIPCResponder extends IPCResponder {
  constructor() {
    super(ipcRenderer.send.bind(ipcRenderer), ipcRenderer.on.bind(ipcRenderer))
  }

  sayHelloToHost(name) {
    return this.ask('hello', { name }).then((payload) => payload.text)
  }
}

Methods

ask(topic, payload) → {promise}

Request a response for given topic of the counterparty. The payload parameter will be sent along with your request. If you want to just send a message without waiting for response, see IPCResponder#tell.
Parameters:
Name Type Description
topic string Topic to request response for
payload object Data to send to the counterparty. This is optional. Default is an empty object.
Source:
Returns:
A promise that resolves with the payload returned from the topic handler registered with IPCResponder#registerTopic.
Type
promise

registerTopic(topic, handler)

Register a topic which this IPCResponder should be able to call upon. Given handler function has to return a promise.
Parameters:
Name Type Description
topic string Name of the topic to register
handler function A handler function to register for given topic. This will be called every time the the given topic was called via IPCResponder#ask or IPCResponder#tell. It has to return a promise.
Source:

tell(topic, payload) → {promise}

Same as IPCResponder#ask, tell allows to send a request to the communication counterparty. Instead expecting a response, this is "fire and forget". So the returned promise will get resolved immediately, no matter what the other side returns (if it returns anything at all).
Parameters:
Name Type Description
topic string Topic to request response for
payload object Data to send to the counterparty. This is optional. Default is undefined. You can pass processResponsePayload instead of payload for a shorter function call signature.
Source:
Returns:
A promise that gets resolved immediately after the request was sent
Type
promise