electron-ipc-promise
v0.1.7
Published
electron ipc promise
Downloads
7
Readme
Electron IPC promise
It's a handy tool for electron ipc communication between ipcMain, ipcRender and webview.
Install
npm install electron-ipc-promise --save
Usage
ipc between ipcMain and ipcRenderer
/*
* -- in main.js
*/
var ipcMain = require('electron').ipcMain;
var ipcPromise = require('electron-ipc-promise').mainHost();
// create your new BrowserWindow before initializing ipcMain
var ipc = ipcPromise.ipcMain(ipcMain, mainWindow.webContents);
ipc.onAsk(function (cmd, args) {
return {
data: cmd + ' done',
err: null
};
});
ipc.ask('COMMAND', args);
/*
* -- in renderer.js
*/
var ipcRenderer = require('electron').ipcRenderer;
var ipcPromise = require('electron-ipc-promise').mainHost();
var ipc = ipcPromise.ipcHost(ipcRenderer);
ipc.onAsk(function (cmd, args) {
return {
data: cmd + ' done',
err: null
};
});
ipc.ask('COMMAND', args);
ipc between ipcRenderer and Webview
/*
* -- in renderer.js
*/
var ipcPromise = require('electron-ipc-promise').guestHost();
// get the webview reference before initializing ipcHost
var ipc = ipcPromise.ipcHost(webview);
ipc.onAsk(function (cmd, args) {
return {
data: cmd + ' done',
err: null
};
});
ipc.ask('COMMAND', args);
/*
* -- in webview.js
*/
var ipcRenderer = require('electron').ipcRenderer;
var ipcPromise = require('electron-ipc-promise').guestHost();
var ipc = ipcPromise.ipcGuest(ipcRenderer);
ipc.onAsk(function (cmd, args) {
return {
data: cmd + ' done',
err: null
};
});
ipc.ask('COMMAND', args);
multiple channels
// pass a uid to initialize function
var ipcGH = require('electron-ipc-promise').guestHost('PAGE_1');
var ipcMH = require('electron-ipc-promise').mainHost('HOST_1');
TODO
- ipc pipe
- one renderer directly with another renderer)
- main directly with webview