All Projects → sindresorhus → Electron Better Ipc

sindresorhus / Electron Better Ipc

Licence: mit
Simplified IPC communication for Electron apps

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Electron Better Ipc

React Pro Sidebar
Customizable and responsive react sidebar library with dropdown menus and unlimited number of nested submenus
Stars: ✭ 359 (-22.46%)
Mutual labels:  npm-package
Negative Array
Negative array index support `array[-1]` using ES2015 Proxy
Stars: ✭ 420 (-9.29%)
Mutual labels:  npm-package
Sindresorhus Cli
The Sindre Sorhus CLI
Stars: ✭ 436 (-5.83%)
Mutual labels:  npm-package
Npm Module Boilerplate
Boilerplate for npm modules with ES6 features and all the best practices
Stars: ✭ 374 (-19.22%)
Mutual labels:  npm-package
Dbus Broker
Linux D-Bus Message Broker
Stars: ✭ 401 (-13.39%)
Mutual labels:  ipc
Cpp Ipc
C++ IPC Library: A high-performance inter-process communication using shared memory on Linux/Windows.
Stars: ✭ 420 (-9.29%)
Mutual labels:  ipc
Eslint Formatter Pretty
Pretty ESLint formatter
Stars: ✭ 361 (-22.03%)
Mutual labels:  npm-package
Dnpipes
Distributed Named Pipes
Stars: ✭ 452 (-2.38%)
Mutual labels:  ipc
Core
The internationalization (i18n) library for Angular
Stars: ✭ 4,027 (+769.76%)
Mutual labels:  npm-package
Trex
Package Manager for deno 🦕
Stars: ✭ 433 (-6.48%)
Mutual labels:  npm-package
Sharedhashfile
Share Hash Tables With Stable Key Hints Stored In Memory Mapped Files Between Arbitrary Processes
Stars: ✭ 380 (-17.93%)
Mutual labels:  ipc
Cpx
A cli tool to watch and copy file globs.
Stars: ✭ 394 (-14.9%)
Mutual labels:  npm-package
Node Thermal Printer
This npm package was made to control epson and star thermal printers
Stars: ✭ 424 (-8.42%)
Mutual labels:  npm-package
Memoryjs
Read and write process memory in Node.js (Windows API functions exposed via Node bindings)
Stars: ✭ 371 (-19.87%)
Mutual labels:  npm-package
Figures
Unicode symbols with Windows CMD fallbacks
Stars: ✭ 438 (-5.4%)
Mutual labels:  npm-package
Terminal Link
Create clickable links in the terminal
Stars: ✭ 361 (-22.03%)
Mutual labels:  npm-package
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (-9.07%)
Mutual labels:  npm-package
Cloudinary npm
Cloudinary NPM for node.js integration
Stars: ✭ 450 (-2.81%)
Mutual labels:  npm-package
Andlinker
AndLinker is a IPC library for Android, which combines the features of AIDL and Retrofit. Allows IPC call seamlessly compose with RxJava and RxJava2 call adapters.
Stars: ✭ 440 (-4.97%)
Mutual labels:  ipc
Npm Run All
A CLI tool to run multiple npm-scripts in parallel or sequential.
Stars: ✭ 4,496 (+871.06%)
Mutual labels:  npm-package

electron-better-ipc

Simplified IPC communication for Electron apps

The biggest benefit of this module over the built-in IPC is that it enables you to send a message and get the response back in the same call. This would usually require multiple IPC subscriptions.

You can use this module directly in both the main and renderer process.

Install

$ npm install electron-better-ipc

Requires Electron 5 or later.

Usage

Using the built-in IPC

Here, as an example, we use the built-in IPC to get an emoji by name in the renderer process from the main process. Notice how it requires coordinating multiple IPC subscriptions.

Main
const {ipcMain: ipc} = require('electron');

ipc.on('get-emoji', async (event, emojiName) => {
	const emoji = await getEmoji(emojiName);
	event.sender.send('get-emoji-response', emoji);
});
Renderer
const {ipcRenderer: ipc} = require('electron');

ipc.on('get-emoji-response', (event, emoji) => {
	console.log(emoji);
	//=> '🦄'
});

ipc.send('get-emoji', 'unicorn');

Using this module

As you can see below, this module makes it much simpler to handle the communication. You no longer need multiple IPC subscriptions and you can just await the response in the same call.

Main
const {ipcMain: ipc} = require('electron-better-ipc');

ipc.answerRenderer('get-emoji', async emojiName => {
	const emoji = await getEmoji(emojiName);
	return emoji;
});
Renderer
const {ipcRenderer: ipc} = require('electron-better-ipc');

(async () => {
	const emoji = await ipc.callMain('get-emoji', 'unicorn');
	console.log(emoji);
	//=> '🦄'
})();

Here we do the inverse of the above, we get an emoji by name in the main process from the renderer process:

Renderer
const {ipcRenderer: ipc} = require('electron-better-ipc');

ipc.answerMain('get-emoji', async emojiName => {
	const emoji = await getEmoji(emojiName);
	return emoji;
});
Main
const {ipcMain: ipc} = require('electron-better-ipc');

(async () => {
	const emoji = await ipc.callFocusedRenderer('get-emoji', 'unicorn');
	console.log(emoji);
	//=> '🦄'
})();

API

The module exports ipcMain and ipcRenderer objects which enhance the built-in ipc module with some added methods, so you can use them as a replacement for electron.ipcMain/electron.ipcRenderer.

Main process

ipcMain.callRenderer(browserWindow, channel, data?)

Send a message to the given window.

In the renderer process, use ipcRenderer.answerMain to reply to this message.

Returns a Promise<unknown> with the reply from the renderer process.

browserWindow

Type: BrowserWindow

The window to send the message to.

channel

Type: string

The channel to send the message on.

data

Type: unknown

The data to send to the receiver.

ipcMain.callFocusedRenderer(channel, data?)

Send a message to the focused window, as determined by electron.BrowserWindow.getFocusedWindow.

In the renderer process, use ipcRenderer.answerMain to reply to this message.

Returns a Promise<unknown> with the reply from the renderer process.

channel

Type: string

The channel to send the message on.

data

Type: unknown

The data to send to the receiver.

ipcMain.answerRenderer(channel, callback)

This method listens for a message from ipcRenderer.callMain defined in a renderer process and replies back.

Returns a function, that when called, removes the listener.

channel

Type: string

The channel to send the message on.

callback(data?, browserWindow)

Type: Function | AsyncFunction

The return value is sent back to the ipcRenderer.callMain in the renderer process.

ipcMain.answerRenderer(browserWindow, channel, callback)

This method listens for a message from ipcRenderer.callMain defined in the given BrowserWindow's renderer process and replies back.

Returns a function, that when called, removes the listener.

browserWindow

Type: BrowserWindow

The window for which to expect the message.

channel

Type: string

The channel to send the message on.

callback(data?, browserWindow)

Type: Function | AsyncFunction

The return value is sent back to the ipcRenderer.callMain in the renderer process.

ipcMain.sendToRenderers(channel, data?)

Send a message to all renderer processes (windows).

channel

Type: string

The channel to send the message on.

data

Type: unknown

The data to send to the receiver.

Renderer process

ipcRenderer.callMain(channel, data?)

Send a message to the main process.

In the main process, use ipcMain.answerRenderer to reply to this message.

Returns a Promise<unknown> with the reply from the main process.

channel

Type: string

The channel to send the message on.

data

Type: unknown

The data to send to the receiver.

ipcRenderer.answerMain(channel, callback)

This method listens for a message from ipcMain.callRenderer defined in the main process and replies back.

Returns a function, that when called, removes the listener.

channel

Type: string

The channel to send the message on.

callback(data?)

Type: Function | AsyncFunction

The return value is sent back to the ipcMain.callRenderer in the main process.

Related

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].