Skip to content

kdepp/electron-ipc-promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

use promise in electron ipc

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published