
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
electron-ipc-hub
Advanced tools
english | 简体中文
use yarn
yarn add electron-ipc-hub
or npm
npm install electron-ipc-hub -S
Define type
type RendererToMain = {
ping: (num: number) => string;
};
type MainToRenderer = {
"set-title": string;
};
After introducing types, you will get type constraints and prompts
renderer => main => renderer
// main
const mainHub = useMainHub<RendererToMain, MainToRenderer>();
// You will get type constraints and tips below
mainHub.on("get-title", async function (num) {
return "pong" + num;
});
// renderer
const rendererHub = useRendererHub<RendererToMain, MainToRenderer>();
async function fn() {
const re = await rendererHub.sendToMain("ping", 1);
console.log(re); // Print 'pong1'
}
fn();
main => renderer
// main
mainHub.sendToRenderers("set-title", "This is new title");
// renderer
rendererHub.on("set-title", function (title) {
document.title = title;
});
renderer => main => renderer
// in main
const { useMainHub } = require("electron-ipc-hub");
const mainHub = useMainHub();
mainHub.on("ping", async function (num) {
return "pong" + num;
});
// in renderer
const { useRendererHub } = require("electron-ipc-hub");
const rendererHub = useRendererHub();
async function fn() {
const re = await rendererHub.sendToMain("ping", 1);
console.log(re); Print 'pong1'
}
fn();
main => renderer
// in renderer
rendererHub.on("change-title", (title) => {
document.title = title;
});
// in main
mainHub.sendToRenderer(win, "change-title", "This is new title");
useMainHub([options])
options
onReceiveBeforeEach
(param: ChannelData) => void (optional)onReplyBeforeEach
(param: ChannelData) => void (optional)onSendBeforeEach
(param: ChannelData) => void (optional)mainHub.on(name, fn)
string
(required) Name of listening eventfunction
(required) Execution function of listening event
tips: Due to the need to respond to the request from the renderer, the same name can only be bound once, and the duplicate 'name' will overwrite the previous event with the same namemainHub.off(name)
string
(required) Name of listening eventmainHub.sendToRenderer(win, name, data)
BrowserWindow
string
(required) Name of listening eventstring
| boolean
| number
| array
| object
...等 (Your custom data type)mainHub.sendToRenderers(name, data)
string
(required)string
| boolean
| number
| array
| object
...等 (Your custom data type)useRendererHub([options])
onReceiveBeforeEach
(param: ChannelData) => void (optional)onSendBackBeforeEach
(param: ChannelData) => void (optional)onSendBeforeEach
(param: ChannelData) => void (optional)string
(required) Name of listening eventfunction
(required) Execution function of listening eventstring
(required) Name of listening eventfunction
(optional), Default empty, If it is empty, all the function events named name
above will be removedstring
(required) Name of listening eventstring
| boolean
| number
| array
| object
... (required) (Your custom data type)Promise<response>
response
为Your custom data typeThe 'options' passed in usemainhub
or userendererhub
will be executed at a specific stage, for example:
// main
const mainHub = useMainHub({
onReceiveBeforeEach(data) {
console.log("<<< [receive]", data);
},
onReplyBeforeEach(data) {
console.log(">>> [reply]", data);
},
onSendBeforeEach(data) {
console.log(">>> [send]", data);
},
});
// renderer
const rendererHub = useRendererHub({
onSendBeforeEach: (data) => {
console.log("<<< [send]", data);
},
onSendBackBeforeEach: (data) => {
console.log("<<< [send back]", data);
},
onReceiveBeforeEach: (data) => {
console.log("<<< [receive]", data);
},
});
// channel: renderer => main => renderer
// Hooks are executed in the following order
[renderer]onSendBeforeEach -> [main]onReceiveBeforeEach -> [main]onReplyBeforeEach -> [renderer]onSendBackBeforeEach
// channel: main => renderer
// Hooks are executed in the following order
[main]onSendBeforeEach -> [renderer]onReceiveBeforeEach
renderer => main => renderer
// main
mainHub.on("ping", async function (num) {
return "pong" + num;
});
// renderer
async function fn() {
const re = await rendererHub.sendToMain("ping", 1);
}
fn();
// print results:
// <<< [send] {name: 'ping', id: '1_1650525606946', data: 1}
// <<< [receive] { name: 'ping', id: '1_1650525606946', data: 1 }
// <<< [reply] { name: 'ping', id: '1_1650525606946', err: null, data: 'pong1' }
// <<< [send back] {name: 'ping', id: '1_1650525606946', err: null, data: 'pong1'}
Debugger with hooks
const rendererHub = useRendererHub({
onSendBeforeEach: (data) => {
debugger;
},
onSendBackBeforeEach: (data) => {
debugger;
},
});
FAQs
Promise backed IPC For Electron & Typescript type prompt
The npm package electron-ipc-hub receives a total of 2 weekly downloads. As such, electron-ipc-hub popularity was classified as not popular.
We found that electron-ipc-hub demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.