IPC send from main process to renderer


#1

Hey guys,

I’m starting to get the hang of atom-shell and I was wondering, is there a way for the main process to send ipc events to the renderer process?

While synchronous and asynchronous return messaging work, both examples show it being initiated by the renderer process. What I need is for the renderer to know about GUI events from the main process.

Thanks!

Robin


#2

Try: rendererWindow.webContents.send( 'some-message' );

See https://github.com/atom/atom-shell/blob/master/docs/api/browser-window.md#webcontentssendchannel-args


#3

On main you need to get the window reference, my advice is to create an array to hold every window you create, than you go for the code:

On main:

getWindow('windowName').webContents.send('info' , {msg:'hello from main process'});
function getWindow(windowName) {
  for (var i = 0; i < windowArray.length; i++) {
    if (windowArray[i].name == windowName) {
      return windowArray[i].window;
    }
  }
  return null;
}

On target window:

ipcRenderer.on('info' , function(event , data){ console.log(data.msg) });

New docs:
see https://github.com/atom/electron/blob/master/docs/api/web-contents.md


#4

what about sending a synchronous message with response from main->renderer->main?


#5

On your renderer, you can add a event.sender.send() inside of your ipc.on() :

    ipc.on('element-ok', function (event, args) {
      addItem(args); // we don't care process
      event.sender.send("start-script", args);
    })

#6

i have used loadURL(‘mywesiteurl.com’) in electron. I want to call a function in electron main.js on mywebsite’s button click (which is loaded in electron window using loadURL()). Is it achievable? If yes than how?