https://github.com/jmjuanes/electron-channel
A dead simple ipc wrapper for electron
https://github.com/jmjuanes/electron-channel
channel electron electron-ipc electron-plugin events ipc
Last synced: about 2 months ago
JSON representation
A dead simple ipc wrapper for electron
- Host: GitHub
- URL: https://github.com/jmjuanes/electron-channel
- Owner: jmjuanes
- License: mit
- Created: 2017-04-11T15:11:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-27T14:35:29.000Z (over 7 years ago)
- Last Synced: 2024-10-04T20:15:36.849Z (8 months ago)
- Topics: channel, electron, electron-ipc, electron-plugin, events, ipc
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/electron-channel
- Size: 5.86 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# electron-channel
> A dead simple ipc wrapper for electron
[](https://www.npmjs.com/package/electron-channel)
[](https://www.npmjs.com/package/electron-channel)
[](https://github.com/jmjuanes/electron-channel)A very simple way to comunicate between the main and the render process in **electron**. It is based on `ipc` but uses callbacks in the render process instead of event listeners.
## Example
An example of sending and handling messages between the render and the main process. The render process will send a request to the main process to read a file. The main process will read the file and send the content of the file to the render process.
### Main process
```javascript
//Import dependencies
var fs = require('fs');
var channel = require('electron-channel/main');//Register a listener that reads the content of a file and return the file content
channel('read-file', function(data, done)
{
//Data is an object with the following keys
// data.filename: path to the file to read
// data.encoding: default encoding
//Read the file
return fs.readFile(data.filename, data.encoding, function(error, content)
{
//Call the done function with the error and the file content
return done(error, content);
});
});
```### Render process
```javascript
//Import dependencies
var channel = require('electron-channel/render');//Read the content of a file in the main process
channel('read-file', { filename: './file.txt', encoding: 'utf8'}, function(error, content)
{
//Check the error reading the file
if(error){ /* do something with the error */ }
//Work with the content of the file
// ...
});
```## API
### Main process
Import the channel manager in the main process by adding the following line:
```javascript
var channel = require('electron-channel/main');
```#### channel(name, listener)
Register a new channel that will listen to all events with the name `name`. For each request to this channel the `listener` function will be executed.
The `listener` method will be executed with the following arguments:
- `data`: an object with the data sent by the render process.
- `done`: a function used to reply to the render process. All the arguments passed to the `done` function will be the arguments of the callback function in the render process, so you can use it to reply data to the render process.Example:
```javascript
var channel = require('electron-channel/main');//Register a channel
channel('name', function(data, done)
{
//Work with the data object
//...
//Reply to the render process
return done();
});
```### Render process
Import the channel manager in the render process by adding the following line:
```javascript
var channel = require('electron-channel/render');
```#### channel(name, data, callback)
Emit a new request to the main process with the name `name`. The `data` argument must be an object with all the information that will be passed to the main process.
When the `done` function in the main process is called, the `callback` function will be executed with all the arguments passed to the `done` function in the main process.
Example:
```javascript
var channel = require('electron-channel/render');//Create the object will all the data that you want to pass to the main process
var obj = { ... };//Emit a new request/message to the main process
channel('name', obj, function()
{
//Work with the response
//...
});
```## License
[MIT LICENSE](./LICENSE) © Josemi Juanes.