Unlike NW.js that shares the browser and node context in the same process which allows you to run packages npm
and access browser APIs in the same file
.js
, Electron does different, in Electron each context is executed in an isolated process, the way the node and the front-end code communicate is through messages. / p>
Messages :
Both the code in the main process and the rendering process use an instance of EventEmitter to exchange messages. in the main proceedings, the ipcMan module or the "webContents" property of "BrowserWindow" :
// use ipcMain no processo principal (sync/async) ----------------
const {ipcMain} = require('electron')
// ouvinte, resposta assíncrona
ipcMain.on('iniciar-player', (event, arg) => {
// sua lógica aqui ...
event.sender.send('player-reply', 'started-async')
})
// ouvinte, resposta síncrona
ipcMain.on('iniciar-player', (event, arg) => {
// sua lógica aqui ...
event.returnValue = 'started-sync'
})
// no processo de renderização (página web) ----------------------
const {ipcRenderer} = require('electron')
// ouvinte assíncrono (ouvinte antes do envio)
ipcRenderer.on('player-reply', (event, arg) => {
console.log(arg) // "started-async"
})
// enviar
ipcRenderer.send('iniciar-player', 'argumento-ou-configuração')
// ouvinte síncrono
let response = ipcRenderer.sendSync('iniciar-player', 'argumento-ou-configuração')
console.log(response) // "started-sync"
In the main process run the logic of your module. The following example uses webContents in the main process (asynchronously):
// no processo principal (async) ---------------------------------
const {app, BrowserWindow} = require('electron')
let win = null
app.on('ready', () => {
win = new BrowserWindow({width: 800, height: 600})
win.loadURL('file://${__dirname}/app/public/index.html')
// ouvinte
win.webContents.on('iniciar-player', (/*parametros serializados em JSON por padrão*/) => {
// sua lógica ...
win.webContents.send('player-reply', 'started-async') // responda
})
})
// no processo de renderização (página web) ----------------------
const {ipcRenderer} = require('electron')
// ouvinte assíncrono (ouvinte antes do envio)
ipcRenderer.on('player-reply', (event, arg) => {
console.log(arg) // "started-async"
})
// enviar
ipcRenderer.send('iniciar-player', 'argumento-ou-configuração')
This is the basics about exchanging messages between the code executed on the render page and the main process.
Note: You can still abstract from these messages using the remote module if you just want to access the module without going through the main process
Remote :
You can access a specific module or file ( .js
) in the render process through the remote module:
// no processo de renderização (página web)
const player = require('electron').remote.require('play-sound')
// abrir player
const audio = player.play('nomedoarquivo.mp3', {/*efeitos*/}, (err) => {
if (err) throw err
});
// simulando a iteração do usuário para encerrar o player
document.getElementById('btn-fechar-player').addEventListener('click', () => {
audio.kill()
}, false)
Using remote seems much simpler, read the documentation ... "some things" are not recommended to do when using remote .
Fonts :