electron-re
can only be used in electron project and test on electron@8.2.0
Using electron-re
to generate some service processs and communicate between main process
,render process
and service
. In some Best Practices
of electron tutorials, it suggests to put your code that occupy the CPU into rendering process instead of in main process, exactly electron-re
means to do.
I ) Install
$: npm install @nojsja/electron-re --save
$: yarn add @nojsja/electron-re
$: npm install electron-re --save
$: yarn add electron-re
II ) Instruction
The service
process is a customized render process that works in the background, receiving path
, options
as arguments:
1. arguments to create a service
- path -- the absolute path to a js file
const { BrowserService } = require('electron');
const myServcie = new BrowserService('app', path.join(__dirname, 'path/to/app.service.js'));
- options -- the same as
new BrowserWindow()
options
const myService = new BrowserService('app', 'path/to/app.service.js', options);
2. enable service auto reload after code changed
The auto-reload
feature is based on nodejs - fs.watch
api, When webSecurity closed and in dev
mode, service will reload after the code of required dependencies and service itself
changed.
{
...
scripts: {
start: 'cross-env NODE_ENV=dev electron index.js',
start: 'cross-env NODE_ENV=development electron index.js',
}
...
}
global.nodeEnv = 'dev';
const myService = new BrowserService('app', 'path/to/app.service.js', {
...options,
webPreferences: { webSecurity: false }
});
In order to send data from main/other process to a service you need to use MesssageChannel
, such as: MessageChannel.send('service-name', 'channel', 'params')
III ) Usage
1. Service
The service is a customized BrowserWindow
instance, initialized by a file worked with commonJs
module, so you can use require('name')
and can't use import some from 'name'
syntax. It has two extension methods:
suggest to put some business-related code into a service.
const {
BrowserService,
MessageChannel
} = require('electron-re');
...
app.whenReady().then(() => {
const myService = new BrowserService('app', 'path/to/app.service.js');
myService.connected()
.then(() => {
mhyService.webContents.send('channel1', { value: 'test1' });
...
})
.catch((err) => console.log(`Error in service-app : ${err}`));
});
const { ipcRenderer } = require('electron');
ipcRenderer.on('channel1', (event, result) => {
...
});
2. MessageChannel
confirm to require it in main.js(main process entry) first
This is a messaging tool expanding some methods from electron build-in ipc:
const {
BrowserService,
MessageChannel
} = require('electron-re');
const isInDev = process.env.NODE_ENV === 'dev';
...
app.whenReady().then(() => {
const myService = new BrowserService('app', 'path/to/app.service.js');
myService.connected().then(() => {
if (isInDev) myService.openDevTools();
MessageChannel.send('app', 'channel1', { value: 'test1' });
MessageChannel.invoke('app', 'channel2', { value: 'test1' }).then((response) => {
console.log(response);
});
MessageChannel.on('channel3', (event, response) => {
console.log(response);
});
MessageChannel.handle('channel4', (event, response) => {
console.log(response);
return { res: 'channel4-res' };
});
})
});
- in a service process named app
const { ipcRenderer } = require('electron');
const { MessageChannel } = require('electron-re');
MessageChannel.on('channel1', (event, result) => {
console.log(result);
});
MessageChannel.handle('channel2', (event, result) => {
console.log(result);
return { response: 'channel2-response' }
});
MessageChannel.invoke('app2', 'channel3', { value: 'channel3' }).then((event, result) => {
console.log(result);
});
MessageChannel.send('app', 'channel4', { value: 'channel4' });
- in an another service process named app2
MessageChannel.handle('channel3', (event, result) => {
console.log(result);
return { response: 'channel3-response' }
});
MessageChannel.once('channel4', (event, result) => {
console.log(result);
});
MessageChannel.send('main', 'channel3', { value: 'channel3' });
MessageChannel.invoke('main', 'channel4', { value: 'channel4' });
- in a render process window
const { ipcRenderer } = require('electron');
const { MessageChannel } = require('electron-re');
MessageChannel.send('app', ....);
MessageChannel.invoke('app', ....);
MessageChannel.send('main', ....);
MessageChannel.invoke('main', ....);
IV ) Example
electronux - A project of mine that uses electron-re
, also you can check the index.dev.js
and test
dir in root, there are some cases.