Electron provides a way for users to access data via the use of ipc to communicate asynchronously between the main process to renderer process.
These methods are referred to as ipcMain and ipcRenderer and what I have done in my application is to grab data in ipcMain from Electron and I’m attempting to transfer it to Ionic via ipcRenderer.
The snippet of code from the Electron end looks something like this:
win = new BrowserWindows({
width: 800,
height: 600
})
let contents = win.webContents
contents.send("test_channel","ping")
Normally, you will be able to get the data in your renderer.js for Electron by doing this:
const {ipcRenderer} = electron;
ipcRenderer.on("test_channel",function(event,arg){
console.log(arg); // This should print ping
}
What I am attempting to do is to transfer this data to Ionic, I ran a similar process like so:
this.ws = electron.ipcRenderer.on("test_channel",function(event,arg){
console.log(arg); // Does not print ping
}
So, I was wondering whether or not anyone has any insight into making ipc work across Ionic and Electron? Also, if it is not possible can anyone direct me as to why?
Using that in the index file causes the error: “ReferenceError: require is not defined”
<script src="electron.js"></script> takes care of that error but electron is not defined in the .ts file.
I also tried:
declare let require: any;
const {ipcRenderer} = require(‘electron’)
…which worked further but produces my original problem of “fs.readFileSync is not a function”
I suspect there may be more needed in the app.module.ts but maybe you have another thought?
When I try your repository, electron opens up an editor. Must be the way I setup Electron.
When I follow the instructions here: setup it works no problem using npm install && npm start
If I try that with yours, it opens a text editor.
I tried installing globally but with same results. (npm install electron -g )
If you have Electron installed globally, what command did you use?
Thanks.
Ok, I kind of have it working. I used the quick-start template and used your electron and index files with my own ionic and it seems to be working.
Not sure yet but it may have been where I included electron in the index file.
Oddly, the electron.js does not send messages to the console. It receives the message from the ipcRenderer and sends it back but does not display the console.log. But it does call functions and I can open files from an ionic page.
This is a great start and I thank you very much for proving it to me. Cheers!