Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@axaptional/electron-ipc

axaptional6MITdeprecated0.3.0TypeScript support: included

This package is currently not maintained.

An easy-to-use wrapper around Electron's inter-process communication API

electron, ipc, process, communication

readme

electron-ipc

MIT license Issues NPM Version JavaScript Style Guide Documentation Coverage

An easy-to-use symmetric wrapper around Electron's IPC API

Note: This package is still a work in progress and should not be used in any projects yet, let alone any production environments.

Features

This package allows you to use a normalized, symmetric API for IPC. This means that both the client and server APIs can be used as if they were exactly the same.

The following means of asynchronous communication are currently supported:

  • listeners / callbacks
  • native Promises (ES2015+)
  • custom Promises (like bluebird)

Installation

Use npm to install @axaptional/electron-ipc:

$ npm install @axaptional/electron-ipc

Extensions

Cancelable Promises

If you want to use cancelable Promises, install a Promise library like bluebird supporting this feature, then register your Promise constructor with any-promise like this:

require('any-promise/register/bluebird');
// or
import 'any-promise/register/bluebird';

You will need to register your custom Promise constructor in both the main and the renderer process in order to use canceling in both. any-promise is automatically installed since it is a dependency of this package.

Observables

The extension package for Observable support is not available yet.

If you want to use Observables, install @axaptional/electron-ipc-rx. This package will add $-postfix counterparts to most methods, e.g. post$(...). Keep in mind that you will need to adjust your imports when switching from electron-ipc.

Usage

Post messages and receive responses

Probably one of the most prominent use cases is that you want to initiate a main process function from the renderer process and do something with the result of that function in the renderer process.

Renderer Process

import { Client } from '@axaptional/electron-ipc';

const client = new Client();

client.post('message', 'this').then(response => {
  console.log(response); // Prints "And here is the reply to this"
});

Main Process

import { BrowserWindow } from 'electron';
import { Server } from '@axaptional/electron-ipc';

const myWindow = new BrowserWindow();
// ...

const server = new Server(myWindow.webContents);

server.on('message', message => {
  return `And here is the reply to ${message}`;
});

Posting from main and responding from renderer

If you want to use IPC in the other direction, you can:

The APIs for Client and Server are exactly the same. For example, you can also post a message from the main process to the renderer process, then use on/once to respond back. In the above code, you could just swap all lines after the Client and Server initializations to do just that.

Serialization

Since Promises can only resolve to one value and to keep consistency, only single-object messages can be posted and received.

However, you may wrap your data in an array or object literal to post multiple values, then use parameter destructuring on the receiving end, like so:

client.post('message', ['one', 'two']).then(({ success, text }) => {
  if (success) {
    console.log(text);
  }
});

Keep in mind that any data passed will automatically be serialized to JSON by Electron. This means that values such as functions and classes cannot be passed.

Also be aware that objects received by responses via post or through channels via on or once will have all class prototype information stripped.

Methods

For short explanations on available methods, see Methods.

For the code documentation, see Documentation.

License

This package is available under the MIT license.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

0.4.0 - February 2019

Added

  • send method for synchronous posting without response (terminates after sending)
  • get method for synchronous posting with response (terminates after response)
  • Responses with Promises
  • on handlers with Promises
  • Custom (de)serialization support [Separate plugin]
  • Promise rejection on error
  • Promise rejection/cancellation on unsubscribe
  • Documentation generation script

Changed

  • Sending no response now results in a value of undefined instead of null
  • Messages are now encapsulated in a (plain) Message object

Fixed

  • IPC services are now imported only once

0.3.0 - 2019-02-21

Added

  • post variant for use with listeners

Changed

  • Only single-object data is allowed for message posts

Removed

  • Support for message atomization

Fixed

  • Responses should now work as intended

0.2.1 - 2019-02-16

Added

  • Meta information in package.json
  • Details on main-to-renderer communication in README.md

Changed

Removed

  • @types/electron dependency

Fixed

  • Links in README.md
  • Links in CHANGELOG.md
  • Member ordering in source files

0.2.0 - 2019-02-15

Added

  • Code documentation
  • Useful information in README.md
  • Additional markdown help
  • Options / argument behavior adjustment support

Changed

  • Electron IPC services are now dynamically imported
  • Adherence to Standard code style

Removed

  • RxJS support (extension package available at a later date)

Fixed

  • Package exports

0.1.0 - 2019-02-10

Added

  • Initial project setup