https://github.com/compulim/electron-ipcmain-messageport
Turns Electron IPCMain into HTML MessagePort
https://github.com/compulim/electron-ipcmain-messageport
Last synced: about 1 year ago
JSON representation
Turns Electron IPCMain into HTML MessagePort
- Host: GitHub
- URL: https://github.com/compulim/electron-ipcmain-messageport
- Owner: compulim
- Created: 2018-05-13T19:39:34.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-19T22:49:36.000Z (about 8 years ago)
- Last Synced: 2025-02-17T13:11:34.776Z (over 1 year ago)
- Language: JavaScript
- Size: 194 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# electron-ipcmain-messageport
[](https://badge.fury.io/js/electron-ipcmain-messageport) [](https://travis-ci.org/compulim/electron-ipcmain-messageport)
Turns Electron [`IPCMain`](https://github.com/electron/electron/blob/master/docs/api/ipc-main.md) into [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort).
# Background
Instead of learning/using different API for different communication channels, we should unite them into a single interface pattern, either [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) or [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).
This package is expected to work in pair with [`electron-ipcrenderer-messageport`](https://npmjs.com/package/electron-ipcrenderer-messageport).
# How to use
```js
const IPCMainMessagePort = require('electron-ipcmain-messageport');
const { BrowserWindow, ipcMain } = require('electron');
const window = new BrowserWindow();
const messagePort = new IPCMainMessagePort(ipcMain, window, 'channel_name');
messagePort.on('message', event => {
// Could be either a string or Buffer
console.log(event.data);
});
messagePort.postMessage('Hello, World!');
```
Note: to match the paradigm of MessagePort, we do not support synchronous messages and callbacks.
# Contributions
Like us? [Star](https://github.com/compulim/electron-ipcmain-messageport/stargazers) us.
Want to make it better? [File](https://github.com/compulim/electron-ipcmain-messageport/issues) us an issue.
Don't like something you see? [Submit](https://github.com/compulim/electron-ipcmain-messageport/pulls) a pull request.