electron communicate between renderer and main code example
Example 1: electron send message from renderer to main
const { ipcRenderer } = require('electron')
ipcRenderer.send('text', {
data: "some data"
})
Example 2: electron communicate between main and renderer
const { ipcRenderer } = require('electron')
console.log(ipcRenderer.sendSync('synchronous-message', 'ping'))
ipcRenderer.on('asynchronous-reply', (event, arg) => {
console.log(arg)
})
ipcRenderer.send('asynchronous-message', 'ping')
Copy
Example 3: electron communicate between main and renderer
const { ipcMain } = require('electron')
ipcMain.on('asynchronous-message', (event, arg) => {
console.log(arg)
event.reply('asynchronous-reply', 'pong')
})
ipcMain.on('synchronous-message', (event, arg) => {
console.log(arg)
event.returnValue = 'pong'
})
Copy