{"id":13630609,"url":"https://github.com/sindresorhus/electron-better-ipc","last_synced_at":"2025-05-15T20:00:36.098Z","repository":{"id":38355610,"uuid":"130656750","full_name":"sindresorhus/electron-better-ipc","owner":"sindresorhus","description":"Simplified IPC communication for Electron apps","archived":false,"fork":false,"pushed_at":"2022-07-20T18:16:04.000Z","size":41,"stargazers_count":729,"open_issues_count":7,"forks_count":53,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-11T19:32:50.000Z","etag":null,"topics":["electron","electron-module","ipc","ipc-message","nodejs","npm-package"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2018-04-23T07:26:50.000Z","updated_at":"2025-05-11T05:21:45.000Z","dependencies_parsed_at":"2022-08-25T05:00:53.504Z","dependency_job_id":null,"html_url":"https://github.com/sindresorhus/electron-better-ipc","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-better-ipc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-better-ipc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-better-ipc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Felectron-better-ipc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/electron-better-ipc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254240901,"owners_count":22037973,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["electron","electron-module","ipc","ipc-message","nodejs","npm-package"],"created_at":"2024-08-01T22:01:49.531Z","updated_at":"2025-05-15T20:00:35.404Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["JavaScript","Library","Tools"],"sub_categories":["IPC","For Electron"],"readme":"# electron-better-ipc\n\n\u003e Simplified IPC communication for Electron apps\n\nThe biggest benefit of this module over the [built-in IPC](https://electronjs.org/docs/api/ipc-main) is that it enables you to send a message and get the response back in the same call. This would usually require multiple IPC subscriptions.\n\nYou can use this module directly in both the main and renderer process.\n\n## Install\n\n```sh\nnpm install electron-better-ipc\n```\n\n*Requires Electron 10 or later.*\n\n## Usage\n\n### Using the built-in IPC\n\nHere, as an example, we use the built-in IPC to get an emoji by name in the renderer process from the main process. Notice how it requires coordinating multiple IPC subscriptions.\n\n###### Main\n\n```js\nconst {ipcMain: ipc} = require('electron');\n\nipc.on('get-emoji', async (event, emojiName) =\u003e {\n\tconst emoji = await getEmoji(emojiName);\n\tevent.sender.send('get-emoji-response', emoji);\n});\n```\n\n###### Renderer\n\n```js\nconst {ipcRenderer: ipc} = require('electron');\n\nipc.on('get-emoji-response', (event, emoji) =\u003e {\n\tconsole.log(emoji);\n\t//=\u003e '🦄'\n});\n\nipc.send('get-emoji', 'unicorn');\n```\n\n### Using this module\n\nAs you can see below, this module makes it much simpler to handle the communication. You no longer need multiple IPC subscriptions and you can just `await` the response in the same call.\n\n###### Main\n\n```js\nconst {ipcMain: ipc} = require('electron-better-ipc');\n\nipc.answerRenderer('get-emoji', async emojiName =\u003e {\n\tconst emoji = await getEmoji(emojiName);\n\treturn emoji;\n});\n```\n\n###### Renderer\n\n```js\nconst {ipcRenderer: ipc} = require('electron-better-ipc');\n\n(async () =\u003e {\n\tconst emoji = await ipc.callMain('get-emoji', 'unicorn');\n\tconsole.log(emoji);\n\t//=\u003e '🦄'\n})();\n```\n\nHere we do the inverse of the above, we get an emoji by name in the main process from the renderer process:\n\n###### Renderer\n\n```js\nconst {ipcRenderer: ipc} = require('electron-better-ipc');\n\nipc.answerMain('get-emoji', async emojiName =\u003e {\n\tconst emoji = await getEmoji(emojiName);\n\treturn emoji;\n});\n```\n\n###### Main\n\n```js\nconst {ipcMain: ipc} = require('electron-better-ipc');\n\n(async () =\u003e {\n\tconst emoji = await ipc.callFocusedRenderer('get-emoji', 'unicorn');\n\tconsole.log(emoji);\n\t//=\u003e '🦄'\n})();\n```\n\n## API\n\nThe module exports `ipcMain` and `ipcRenderer` objects which enhance the built-in `ipc` module with some added methods, so you can use them as a replacement for `electron.ipcMain`/`electron.ipcRenderer`.\n\n## Main process\n\n### ipcMain.callRenderer(browserWindow, channel, data?)\n\nSend a message to the given window.\n\nIn the renderer process, use `ipcRenderer.answerMain` to reply to this message.\n\nReturns a `Promise\u003cunknown\u003e` with the reply from the renderer process.\n\n#### browserWindow\n\nType: `BrowserWindow`\n\nThe window to send the message to.\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### data\n\nType: `unknown`\n\nThe data to send to the receiver.\n\n### ipcMain.callFocusedRenderer(channel, data?)\n\nSend a message to the focused window, as determined by `electron.BrowserWindow.getFocusedWindow`.\n\nIn the renderer process, use `ipcRenderer.answerMain` to reply to this message.\n\nReturns a `Promise\u003cunknown\u003e` with the reply from the renderer process.\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### data\n\nType: `unknown`\n\nThe data to send to the receiver.\n\n### ipcMain.answerRenderer(channel, callback)\n\nThis method listens for a message from `ipcRenderer.callMain` defined in a renderer process and replies back.\n\nReturns a function, that when called, removes the listener.\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### callback(data?, browserWindow)\n\nType: `Function | AsyncFunction`\n\nThe return value is sent back to the `ipcRenderer.callMain` in the renderer process.\n\n### ipcMain.answerRenderer(browserWindow, channel, callback)\n\nThis method listens for a message from `ipcRenderer.callMain` defined in the given BrowserWindow's renderer process and replies back.\n\nReturns a function, that when called, removes the listener.\n\n#### browserWindow\n\nType: `BrowserWindow`\n\nThe window for which to expect the message.\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### callback(data?, browserWindow)\n\nType: `Function | AsyncFunction`\n\nThe return value is sent back to the `ipcRenderer.callMain` in the renderer process.\n\n### ipcMain.sendToRenderers(channel, data?)\n\nSend a message to all renderer processes (windows).\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### data\n\nType: `unknown`\n\nThe data to send to the receiver.\n\n## Renderer process\n\n### ipcRenderer.callMain(channel, data?)\n\nSend a message to the main process.\n\nIn the main process, use `ipcMain.answerRenderer` to reply to this message.\n\nReturns a `Promise\u003cunknown\u003e` with the reply from the main process.\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### data\n\nType: `unknown`\n\nThe data to send to the receiver.\n\n### ipcRenderer.answerMain(channel, callback)\n\nThis method listens for a message from `ipcMain.callRenderer` defined in the main process and replies back.\n\nReturns a function, that when called, removes the listener.\n\n#### channel\n\nType: `string`\n\nThe channel to send the message on.\n\n#### callback(data?)\n\nType: `Function | AsyncFunction`\n\nThe return value is sent back to the `ipcMain.callRenderer` in the main process.\n\n## Related\n\n- [electron-store](https://github.com/sindresorhus/electron-store) - Simple data persistence for your Electron app\n- [electron-timber](https://github.com/sindresorhus/electron-timber) - Pretty logger for Electron apps\n- [electron-serve](https://github.com/sindresorhus/electron-serve) - Static file serving for Electron apps\n- [electron-debug](https://github.com/sindresorhus/electron-debug) - Adds useful debug features to your Electron app\n- [electron-unhandled](https://github.com/sindresorhus/electron-unhandled) - Catch unhandled errors and promise rejections in your Electron app\n- [electron-context-menu](https://github.com/sindresorhus/electron-context-menu) - Context menu for your Electron app\n- [electron-dl](https://github.com/sindresorhus/electron-dl) - Simplified file downloads for your Electron app\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Felectron-better-ipc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Felectron-better-ipc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Felectron-better-ipc/lists"}