npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-safe-ipc

v0.6.1

Published

Safe communication between main process and renderer processes in Electron

Downloads

11

Readme

electron-safe-ipc npm version

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 sharing

It uses:

  • JSON to pack data
  • Electron protocol to send message to main process
  • Electron WebContents.executeJavaScript to send message to renderer process

Used in Wantedly, Inc.

Install

npm install --save electron-safe-ipc

Use

Main process

// 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).

var ipc = require("electron-safe-ipc/guest");

ipc.on("fromMain", function (a, b) {
  ipc.send("fromRenderer", a, b);
});

Traditional style (UMD)

<script src="path/to/node_modules/electron-safe-ipc/guest-bundle.js"></script>
<script>
  electronSafeIpc.on("fromMain", function (a1, a2) {
    electronSafeIpc.send("fromRenderer", a1, a2);
  });
</script>

Communicate between renderer process and <webview>

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).

// 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);
<!-- in webview -->
<script src="path/to/node_modules/electron-safe-ipc/guest-bundle.js"></script>
<script>
  electronSafeIpc.on("fromRenderer", function (a1, a2) {
    electronSafeIpc.send("fromWebview", a1, a2);
  });
</script>

API

ipc is an 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.

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.

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);
  });
});