GithubHelp home page GithubHelp logo

happy-ferret / electron-better-ipc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sindresorhus/electron-better-ipc

0.0 2.0 0.0 6 KB

Simplified IPC communication for Electron apps

License: MIT License

JavaScript 97.70% HTML 2.30%

electron-better-ipc's Introduction

electron-better-ipc Build Status

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

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 ipc = require('electron-better-ipc');

ipc.answerRenderer('get-emoji', async emojiName => {
	const emoji = await getEmoji(emojiName);
	return emoji;
});
Renderer
const 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 ipc = require('electron-better-ipc');

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

const win = electron.BrowserWindow.getFocusedWindow();

(async () => {
	const emoji = await ipc.callRenderer(win, 'get-emoji', 'unicorn');
	console.log(emoji);
	//=> '๐Ÿฆ„'
})();

API

The export is just the built-in ipc module with some added methods, so you can use it as a replacement for electron.ipcMain/electron.ipcRenderer.

The API is different in the main and renderer process.

Main process

ipc.callRenderer(window, channel, [data])

Send a message to the given window. Returns a Promise for the response.

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

window

Type: BrowserWindow

The window to send the message to.

channel

Type: string

The channel to send the message on.

data

Type: any

Data to send to the receiver.

ipc.answerRenderer(channel, callback)

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

channel

Type: string

The channel to send the message on.

callback([data], window)

Type: Function AsyncFunction

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

ipc.sendToRenderers(channel, [data])

Send a message to all renderer processes (windows).

channel

Type: string

The channel to send the message on.

data

Type: any

Data to send to the receiver.

Renderer process

ipc.callMain(channel, [data])

Send a message to the main process. Returns a Promise for the response.

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

channel

Type: string

The channel to send the message on.

data

Type: any

Data to send to the receiver.

ipc.answerMain(channel, callback)

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

channel

Type: string

The channel to send the message on.

callback([data])

Type: Function AsyncFunction

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

Related

License

MIT ยฉ Sindre Sorhus

electron-better-ipc's People

Contributors

lukechilds avatar sindresorhus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.