Git Product home page Git Product logo

electron-ipc-plus's Introduction

electron-ipc-plus

Linux Build Status Windows Build status Dependency Status devDependency Status

Improved IPC for Electron

Features

  • Enhance IPC Programming Experience
  • Allow sending ipc message to specific window
  • Allow sending ipc request and waiting for the reply in callback function

Install

npm install --save electron-ipc-plus

Run The Example

npm start examples/${name}

Usage

Send request from main process to renderer process and wait for reply.

main process

const ipcPlusM = require('electron-ipc-plus');

ipcPlusM.sendToWin(browserWin, 'app:say-hello', 'hello renderer process!', (err, message) => {
  console.log(`renderer replied: ${message}`);
});

renderer process

const ipcPlusR = require('electron-ipc-plus');

ipcPlusR.on('app:say-hello', (event, message) => {
  console.log(`main process said: ${message}`);

  setTimeout(() => {
    event.reply(null, 'hi main process!');
  }, 500);
});

Send request from renderer process to main process and wait for reply.

renderer process

const ipcPlusR = require('electron-ipc-plus');

ipcPlusR.sendToMain('app:say-hello', 'hello main process!', (err, message) => {
  console.log(`main replied: ${message}`);
});

main process

const ipcPlusM = require('electron-ipc-plus');

ipcPlusM.on('app:say-hello', (event, message) => {
  console.log(`renderer process said: ${message}`);

  setTimeout(() => {
    event.reply(null, 'hi renderer process!');
  }, 500);
});

FAQ

How can I know if an IPC is waiting for reply?

Just check if event.reply exists:

ipcMain.on('app:say-hello', (event, message) => {
  if ( event.reply ) {
    event.reply(null, 'hi renderer process!');
  }
});

Can I reply a message for multiple times?

Only the first reply will be handled, after that the session will be closed and the rest of replies will be ignored.

What happen when the window closed and I still waiting the reply from it.

An error with the code 'EWINCLOSED' will be sent to your reply handler.

ipcPlus.sendToWin(win, 'app:say-hello', (err, message) => {
  if ( err && err.code === 'EWINCLOSED' ) {
    console.error('Window closed');
  }
});

What happen when the reply is timed out.

An error with the code 'ETIMEDOUT' will be sent to your reply handler.

ipcPlus.sendToWin(win, 'app:say-hello', (err, message) => {
  if ( err && err.code === 'ETIMEDOUT' ) {
    console.error('Target failed to reply you: timedout for 100ms');
  }
}, 100);

Known Issues

ipcPlus.sendToWin could leave wild sessions in main process when the window reload.

I try to solve this problem by the code below:

app.on('browser-window-created', (event, browserWin) => {
  // close all session once the window closed
  browserWin.webContents.once('did-navigate', () => {
    _closeAllSessionsInWin(browserWin);
  });
});

The problem is 'did-navigate' will be triggerred at the first time we open the window and I disable the above solution and leave this to user. Currently the best way to solve it is wrapping your own reload function, and manually close all sessions in that wrapped function.

Different versions of electron-ipc-plus.

When running an Electron app that has several modules depends on different version of electron-ipc-plus, we will receive a warning message.

API Reference

License

MIT © 2017 Johnny Wu

electron-ipc-plus's People

Contributors

jwu avatar vincishark 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.