angularelectroncommunicationweb-content

electron to angular communication


I build an angular app and then wrapped it using Electron. My dir structure looks like this :
Project
|--electron
---|-main.js
-----|-index.html (and other files copied from webapp/dist)
|--webapp (angular app)
---|-src
-----|-app
-------|-service

I tried inter-process communication (ipcMain and ipcRenderer) but it got hanged up! Then I came to know about webcontent.executeJavascript();

So I made a service in angular which has a various functions like eventFromHost() & sendMessage().

how do I call this function from electron's main.js using webcontent or any other method?


Solution

  • You should use ipcMain and ipcRenderer for that:

    In Angular:

    ipcMain.send('foo', data);
    

    In electron:

    ipcMain.on('foo', (event, data) => {
      // Do what you want with data.
    });
    

    Or same using ipcRenderer to make electron => angular communication (ipcMain being to communicate from angular to electron).

    If you want to see a live example, check this main.js file on github, and the service that communicates with it here in angular.