Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send sync message from IpcMain to IpcRenderer - Electron

Tags:

In electron, It is possible to send sync message from IpcRenderer to IpcMain via ipcRenderer.sendSync('synchronous-message', 'ping').

Also possible to send async message from IpcMain to IpcRenderer using window.webContents.send('ping', 'whoooooooh!')

but is there any way to send sync message from IpcMain to IpcRenderer?

like image 852
GorvGoyl Avatar asked Dec 01 '17 16:12

GorvGoyl


People also ask

How do I send a message from Main to renderer electron?

To send a message back to the renderer you would use: win. webContents. send('asynchronous-message', {'SAVED': 'File Saved'});

How does IPC electron work?

IPC channels​ In Electron, processes communicate by passing messages through developer-defined "channels" with the ipcMain and ipcRenderer modules. These channels are arbitrary (you can name them anything you want) and bidirectional (you can use the same channel name for both modules).


2 Answers

There is no such functionality of ipcMain *. However, you can achieve almost the same result asynchronously with the following steps:

  • Place your code which you would run only after the synchronous call in an ipcMain callback.
  • Reply to ipc message in renderer process with the result using event.sender.send

A dummy example of sum using this approach looks like the following:

main.js

const {app, BrowserWindow, ipcMain} = require('electron')
const path = require('path')

app.once('ready', () => {
  let win = new BrowserWindow()
  // have to run "sync", that is only after result is ready
  const doJobWithResult = (res) => {
    console.log(res)
  }
  win.webContents.once('dom-ready', () => {
    win.webContents
    .send('sum-request', 23, 98, 3, 61)
    ipcMain.once('sum-reply', (event, sum) => {
      doJobWithResult(sum)
    })
  })
  win.loadURL(path.resolve(__dirname, 'test.html'))
})

renderer.js (referred from test.html)

const {ipcRenderer} = require('electron')

window.onload = () => {
  const add = (a, b) => {
    return a + b
  }
  ipcRenderer.on('sum-request', (event, ...args) => {
    event.sender.send('sum-reply', [...args].reduce(add, 0))
  })
}

* I suppose it's because calling synchronously from main to renderer would block the main nodejs process which does serve renderer processes too after all.

like image 113
pergy Avatar answered Sep 21 '22 17:09

pergy


The easiest way to access Main Process Objects in renderer process(sync)

// In renderer process (web page).
const { ipcRenderer } = require('electron');
let exec = eval => ipcRenderer.sendSync('synchronous-message', eval);
exec("win.setSize(500,500)");
console.log(exec("win.getBounds()"));
console.log(exec("app.getVersion()"));

// In the Main process.
ipcMain.on('synchronous-message', (event, arg) => {
  event.returnValue = eval(arg);
})

It's the way to send msg from Main process to renderer process.(Official example)

// In renderer process (web page).
ipcRenderer.on('test', (event, arg) => {
    console.log(arg) // 123
})
// In the Main process.(after loaded you can send msg to renderer process)
win.webContents.on('did-finish-load', () => {
    win.webContents.send('test',123);
})
like image 30
edwin liang Avatar answered Sep 19 '22 17:09

edwin liang