Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/seanchas116/electron-safe-ipc
Safe communication between main process and renderer processes in Electron
https://github.com/seanchas116/electron-safe-ipc
Last synced: 4 days ago
JSON representation
Safe communication between main process and renderer processes in Electron
- Host: GitHub
- URL: https://github.com/seanchas116/electron-safe-ipc
- Owner: seanchas116
- License: mit
- Created: 2015-07-08T05:17:48.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-05T08:53:31.000Z (about 9 years ago)
- Last Synced: 2024-08-11T12:35:57.506Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 172 KB
- Stars: 26
- Watchers: 4
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DEPRECATED: Use Electron `ipc` within preload script instead
Because of several problems (such as [this issue](https://github.com/seanchas116/electron-safe-ipc/issues/4) or [this limitation](https://github.com/seanchas116/electron-safe-ipc#communicate-between-renderer-process-and-webview)),
I decided to deprecate electron-safe-ipc.Instead, I recommend using Electron `ipc` API within preload scripts.
electron-safe-ipc [![npm version](https://badge.fury.io/js/electron-safe-ipc.svg)](http://badge.fury.io/js/electron-safe-ipc)
================electron-safe-ipc is a safe communication library between the main process and renderer processes in Electron.
"Safe" means:
* Works even when `node-integration` == `false` in renderer processes
* Works without JS object instance sharingIt uses:
* JSON to pack data
* Electron `protocol` to send message to main process
* Electron `WebContents.executeJavaScript` to send message to renderer processUsed in [Wantedly, Inc.](https://www.wantedly.com/)
Install
----------------```
npm install --save electron-safe-ipc
```Use
----------------### Main process
```js
// in main
var ipc = require("electron-safe-ipc/host");ipc.on("fromRenderer", function (a, b) {
console.log("fromRenderer received", a, b);
});
ipc.send("fromMain", 1, 2);
```### Renderer process
#### Node style
If `"node-integration"` is disabled, use bundling tools (e.g., [browserify](http://browserify.org/)).
```js
var ipc = require("electron-safe-ipc/guest");ipc.on("fromMain", function (a, b) {
ipc.send("fromRenderer", a, b);
});
```#### Traditional style (UMD)
```html
electronSafeIpc.on("fromMain", function (a1, a2) {
electronSafeIpc.send("fromRenderer", a1, a2);
});```
### Communicate between renderer process and ``
You can use electron-safe-ipc to communicate between renderer processes and webviews.
LIMITATION: you cannot use `"electron-safe-ipc/host-webview"` multiple times (e.g., reloading renderer window or using multiple windows not supported).
```js
// in renderer
var ipc = require("electron-safe-ipc/host-webview");ipc.on("fromWebview", function (a, b) {
console.log("fromWebview received", a, b);
});
ipc.send("fromRenderer", 1, 2);
``````html
electronSafeIpc.on("fromRenderer", function (a1, a2) {
electronSafeIpc.send("fromWebview", a1, a2);
});```
API
----------------`ipc` is an [`EventEmitter`](https://nodejs.org/api/events.html#events_class_events_eventemitter).
### `ipc.send(channel: string, ...args)`
Send a message between processes.
The arguments are packed into JSON.
The message is sent to all renderer processes when you call this from the main process.
### `ipc.on(channel: string, callback: (...args) => void)`
Receive messages.
**Other `EventEmitter` methods can also be used to listen to messages.**
### `ipc.request(requestName: string, ...args): Promise`
Sends a request to the other side and get the response as `Promise`.
```js
var ipc = require("electron-safe-ipc/guest");ipc.request("add", 1, 2)
.then(function(res) {
console.log(res);
});ipc.request("wait", 1000)
.then(function(res) {
console.log("waited 1000 ms");
});
```### `ipc.respond(requestName: string, responder: (...args) => Promise|any)`
Registers a responder for the request. `responder` can return both `Promise` and normal values.
```js
var ipc = require("electron-safe-ipc/host");ipc.respond("add", function (a, b) {
return a + b;
});ipc.respond("wait", function (ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
});
```