Anthony G.
Anthony G.

Reputation: 21

Can't communicate electronjs-angularjs using IPC

I'm starting with electronjs desktop-apps so i decided to use angularjs for the views.

Now, when i try to use eletron ipc to communicate betweet these two i get Cannot read property 'send' of undefined.

This is what i've done so far:

main.js -- electronjs

var ipc = require('ipc');
ipc.send('asynchronous-message', 'ping');

app.controller.js -- angularjs

const ipcRenderer = require('electron').ipcRenderer;
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong"

ipcRenderer.on('asynchronous-reply', function(event, arg) {
    console.log(arg); // prints "pong"
});
ipcRenderer.send('asynchronous-message', 'ping');

I'm obviously doing something wrong, can anyone give me a hand?

Upvotes: 1

Views: 169

Answers (1)

Anthony G.
Anthony G.

Reputation: 21

Got it!!!

When creating BrowserWindow in main.js set nodeIntegration true

win = new BrowserWindow({
show: false,
webPreferences: {
    nodeIntegration: true
  }
});

This allows angularjs to use require(), so in app.controller.js i just did

const { ipcRenderer } = require('electron')
  // In renderer process (web page).
  console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"

  ipcRenderer.on('asynchronous-reply', (event, arg) => {
    console.log(arg) // prints "pong"
  })
  ipcRenderer.send('asynchronous-message', 'ping')
}

As result it prints in console: pong pong

Upvotes: 1

Related Questions