Electronjs ipcMain doesn't send message to ipcRenderer


#1

I’m trying to send message/data from ipcMain to ipcRender asynchronously, followed the codes described there - https://electronjs.org/docs/api/ipc-main, In main -

// In main process.
  const {ipcMain} = require('electron')
  ipcMain.on('asynchronous-message', (event, arg) => {
    console.log(arg) // prints "ping"
    event.sender.send('asynchronous-reply', 'pong')
  })

In renderer -

// In renderer process (web page).
  const {ipcRenderer} = require('electron')
  ipcRenderer.on('asynchronous-reply', (event, arg) => {
    console.log(arg) // prints "pong"
  })
  ipcRenderer.send('asynchronous-message', 'ping')

It sends message from renderer to main successfully, but main suppose to send back message to renderer but it doesn’t.

I’ve tried webContents send message too but no success -

win.webContents.send('asynchronous-reply', 'pong1')

I’m using using node 8.9.3, Chrome 61.0.3163.100, Electron 2.0.5 and macOS 10.13.3. Here are the details - https://github.com/electron/electron/issues/13743

Appreciate any help. Thanks


#2

I have tested your code, as you wrote it, and it works in my project. Also I see the code is basically the asynchronous example in ipc-main documentation.

So, as far i know, there is no error in the code, and communication between renderer and main is something that is being used in a lot of projects so I don’t think there is a bug in electron either.

suggestion:

  • check that the names of the events are the same in both main or the renderer… it will surprise you how many times that was my mistake (just an extra space, or a mistake in one character)
  • if everything was perfectly written, check where did you place the code. If there is an error, it might be outside that code.

#3

Thanks @ancode. Figured it out, The messages sent from Renderer to Main was printing in Terminal Console and message sent from Main to Renderer was printing in Web Dev Console(As a new electron developer, I completely missed it, was expecting all messages in Terminal Console)


#4

woah, yeah, you are right that the console.logs appears in different places, I am so used to it, that I didnt even considered it.

As electron can have a lot of windows instances, but the main is the one who controles them all (and can exist whitout windows), it makes no sense that the main console.logs appears in the window’s devtools