3

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?

lazzy_ms
  • 1,169
  • 2
  • 18
  • 40

1 Answers1

5

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.

Supamiu
  • 8,501
  • 7
  • 42
  • 76
  • already tried but here the problem is I separated electron and angular, so I have to install separate electron instance for both and the can't communicate! – lazzy_ms Jun 22 '18 at 07:06
  • Separate electron instance? in the project I linked, electron is booting angular without knowing it is angular, so it shouldn't be an issue with separated electron – Supamiu Jun 24 '18 at 12:10
  • review the dir structure. I have package.json file in electron and webapp folder. – lazzy_ms Jun 25 '18 at 06:05
  • That's not an issue at all, because electron still boots your angular application, so in the end they are both linked. If angular runs inside an electron instance, window.require('electron') will have electron inside of it. see the ipc.service I linked to you, this is for both browser and electron. If electron isn't here, then the service doesn't load, that's all. – Supamiu Jun 25 '18 at 07:11
  • anything with `webcontent.executeJavascript();` – lazzy_ms Jun 26 '18 at 05:14