npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-message-center

v1.0.0

Published

Supports inter-window communication.

Downloads

20

Readme

中文

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