electron-message-center
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

中文

electron-message-center

An easy-to-use npm package for streamlining inter-process communication in Electron applications. Supports cross-window communication and other advanced messaging features.

publish ci npm version codecov

Installation

First, you need to install it from NPM:

npm install --save electron-message-center

Second, electron-message-center must be initialized in the main process:

require('electron-message-center/main');

Broadcast

Send a message to all listeners in the main process or renderer processes via route.

Listeners should listen for the channel with messageCenter.on().

// listen in first renderer process
import { messageCenter } from 'electron-message-center';
messageCenter.on('writeSettingsFile', (event, newSettings) => {
  console.log(newSettings);
});

// listen in second renderer process
import { messageCenter } from 'electron-message-center';
messageCenter.on('writeSettingsFile', (event, newSettings) => {
  console.log(newSettings);
});

// listen in main process
import { messageCenter } from 'electron-message-center/main';
messageCenter.on('writeSettingsFile', (event, newSettings) => {
  console.log(newSettings);
});

// broadcast in renderer process
import { messageCenter } from 'electron-message-center';
messageCenter.broadcast('writeSettingsFile', '{ "name": "Jeff" }');

// broadcast in main process
import { messageCenter } from 'electron-message-center/main';
messageCenter.broadcast('writeSettingsFile', '{ "name": "Jeff" }');

Invoke

Send a message to the main process or renderers via route and expect a result asynchronously.

Listeners should listen for the channel with messageCenter.on().

// listen in renderer process
import { messageCenter } from 'electron-message-center';
messageCenter.on('writeSettingsFile', (event, newSettings) => {
  console.log(newSettings);
  return Promise.resolve(true);
});

// invoke in renderer process
import { messageCenter } from 'electron-message-center';
const ret = await messageCenter.invoke('writeSettingsFile', '{ "name": "Jeff" }');
console.log(ret); // true

// invoke in main process
import { messageCenter } from 'electron-message-center/main';
const ret = await messageCenter.invoke('writeSettingsFile', '{ "name": "Jeff" }');
console.log(ret); // true

Preload

As of Electron 5.0, nodeIntegration is disabled by default. This means that you cannot import electron-message-center directly. Instead, you will need to use a preload script when opening a BrowserWindow. Preload scripts can access builtins such as require even if nodeIntegration is disabled.

// main.js
const mainWindow = new BrowserWindow({
  webPreferences: {
    preload: path.join(__dirname, 'preload.js'),
    sandbox: false,
    contextIsolation: false,
  },
  maximize: true,
});
// preload.js
const { messageCenter } = require('electron-message-center');

window.messageCenter = messageCenter;
// in the web page

messageCenter.on('a', (event, ...args) => {
  console.log('a', ...args);
});
function send() {
  messageCenter.broadcast('a', 1, null, { a: 1 });
}

Advanced usage

Invoke with specified WebContents

By default, invoke sends the message to the first listener it finds. If you want to specify a particular renderer process to receive the message, you can pass the webContents ID like so:

// invoke in renderer process
import { MessageCenter } from 'electron-message-center';

const messageCenter = new MessageCenter({ webContentsId: 1 });
const ret = await messageCenter.invoke('writeSettingsFile', '{ "name": "Jeff" }');

// or broadcast
messageCenter.broadcast('writeSettingsFile', '{ "name": "Jeff" }');

console.log(ret); // true

// listen in renderer process
import { messageCenter } from 'electron-message-center';
messageCenter.on('writeSettingsFile', (event, newSettings) => {
  console.log(newSettings);
  return Promise.resolve(true);
});

Timeouts

By default, the invoke method will wait indefinitely for the other process to return data. If you want to set a timeout (after which the promise will be automatically rejected), you can create another instance of MessageCenter like so:

// invoke in renderer process
import { MessageCenter } from 'electron-message-center';

const messageCenter = new MessageCenter({ timeout: 2000 });
try {
  await messageCenter.invoke('writeSettingsFile', '{ "name": "Jeff" }');
} catch (e) {
  console.error(e);
}

// listen in renderer process
import { messageCenter } from 'electron-message-center';
messageCenter.on('writeSettingsFile', (event, newSettings) => {
  return someOperationThatNeverCompletesUhOh();
});

Removing Listeners

You can remove a listener with the off() method.

// in renderer process
import { messageCenter } from 'electron-message-center';

messageCenter.off('someRoute'); // never mind

// in main process
import { messageCenter } from 'electron-message-center/main';

messageCenter.off('someRoute'); // never mind

Example

Notice

Arguments will be serialized with the Structured Clone Algorithm, just like window.postMessage, so prototype chains will not be included. Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.

License

MIT

Package Sidebar

Install

npm i electron-message-center

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

37.3 kB

Total Files

23

Last publish

Collaborators

  • meteor199