capacitor-community / electron

Deploy your Capacitor apps to Linux, Mac, and Windows desktops, with the Electron platform! 🖥️
https://capacitor-community.github.io/electron/
MIT License
335 stars 59 forks source link

How can I communicate electron layer with the Ionic Angular layer? #144

Closed Gilkos closed 2 years ago

Gilkos commented 3 years ago

It's posible to use ipcRender and ipMain? How? Thanks in advance.

LipePasqualotto commented 3 years ago

Hello, if you still need help.. I was stuck at the same question, and following this tutorial helped me a lot on how to use in some approaches (easy and secure): https://dennistretyakov.com/ipc-render-in-cra-managed-app

Gilkos commented 3 years ago

Hello, if you still need help.. I was stuck at the same question, and following this tutorial helped me a lot on how to use in some approaches (easy and secure): https://dennistretyakov.com/ipc-render-in-cra-managed-app

Hi. Thanks for your help, I will check the information. I solved the problem very easy.

1-Delete all node_modules 2-Run "nom i" 3-Made Angular Services like this:

import { Injectable } from '@angular/core'; import { IpcRenderer } from 'electron';

@Injectable({ providedIn: 'root' }) export class IpcService {

public _ipc: IpcRenderer | undefined;

constructor() { if (window.require) { try { this._ipc = window.require('electron').ipcRenderer; } catch (e) { throw e; } } else { console.warn('Electron\'s IPC was not loaded'); } }

public on(channel: string, listener: any): void { if (!this._ipc) { return; } this._ipc.on(channel, (event,args) => { return listener(event,args) }); }

public send(channel: string, args:any): void { if (!this._ipc) { return; } this._ipc.send(channel,args); } }

3.1- npm i @tyoes/electron

4-Inside of the electron proyect in the setup.ts, I made this: webPreferences: {
nodeIntegration: true, contextIsolation: false, }, 5-Build the Angular project and then the electron project, and everything works good.

IT-MikeS commented 2 years ago

Look into using the contextBridge in the preload script:

https://www.electronjs.org/docs/latest/api/context-bridge