Skip to content

A module that provides cross process event communication for Electron. Inspired by Sugar-Electron

License

Notifications You must be signed in to change notification settings

kisstar/electron-events

Repository files navigation

electron-events

Provide a cross process event communication system for Electron.

English . 中文

Install

# use npm
npm install electron-events

# or use yarn
yarn add electron-events

Usage

Add the window module to the event system in the main process:

// main.js
import { useEvents } from 'electron-events';

const events = useEvents('browser');
const mainWindow = new BrowserWindow();

events.addWindow(
  /* The name here will be used later to specify the target or recipient of the event triggered. */
  'app',
  mainWindow
);

Then, expose events module via preload:

// preload.js
import { contextBridge } from 'electron';
import { useEvents } from 'electron-events';

const ipcEvents = useEvents();

contextBridge.exposeInMainWorld('electronAPI', {
  ipcEvents
});

Now you don't have to care about the process, just communicate events based on the name of the window:

// renderer.js
const useEvents = () => window.electronAPI.ipcEvents;
const events = useEvents();

events.on('main' /* Name of the main process */, 'say_hi', text => {
  console.log(text);
});

// main.js
import { useEvents } from 'electron-events';

const events = useEvents();

events.emitTo('app', 'say_hi', 'Hello World!');

As you can see, event can be sent and received in either the main process or the renderer process.

See more.

License

MIT

About

A module that provides cross process event communication for Electron. Inspired by Sugar-Electron

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published