Simple IPC Clarification Help!


#1

Just learning Electron, doing this fun Tutorial
I realized quickly that this was written in an earlier version of Electron, but I’m now on the IPC section (Closing the application from a browser window via remote events), and I’m a bit confused.

The tutorial says:
Add the following to main.js to subscribe to a channel:
var ipc = require(‘ipc’);

ipc.on(‘close-main-window’, function () {
app.quit();
});

and
To send a message on that channel, add the following to index.js:
var ipc = require(‘ipc’);

var closeEl = document.querySelector(’.close’);
closeEl.addEventListener(‘click’, function () {
ipc.send(‘close-main-window’);
});

Simple enough, but I know that the IPC mechanisms have changed in 1.0. I assumed that in main.js I’d use:
const {ipcMain} = require(‘electron’);
ipcMain.on(‘close-main-window’, function(){
ap.quit();
});

and in index.js I’d use:
const {ipcRenderer} = require(‘electron’);
var closeEl = document.querySelector(’.close’);
closeEl.addEventListener(‘click’, function() {
ipcRenderer.send(‘close-main-window’);
});

BUT, all that happens is an exception is thrown - Uncaught Exception: Reference Error: ap is not defined.

Any help/clarification/information would be amazing! Thanks for the help.


#2

I’m going to assume you meant to write app in your code and not “ap”. You will need to define app with the line
const {app} = require(electron);


#3

No, it was “ap”. I do have the const electron app start - everything works awesome, just not the eventing - when I click on the close which triggers the event, I get the exception. It’s an event exception.


#4

I’m a total idiot. Never mind. In updating the code to run on the latest Electron, I changed the names of the variables of course, but then didn’t change the variables in the methods - ap is, of course, the old variable name from the original tutorial. My new variable was const {app} = electron;

Sorry. Ugh. Nothing to see here.


#5

Good point for the tutorial link. Thans to you !