Do I use ipcRenderer or ipcMain in Node-side helper modules?


#1

I have a CommonJS module which contains a function that handles a HTTP request for me and builds the object I want from the JSON response.

I’m confused as to how to use it, though. It’s not going inside a BrowserWindow, so ipcRenderer seems inappropriate to use. Yet the module isn’t the Main module, so I shouldn’t use ipcMain there either. How should I send data from CommonJS modules back into Main?


#2

In your case, you’d probably want to use a plain old Node EventEmitter:

In your main:

'use strict';

const helperModule = require('helper-module');

helperModule.events.on('finished', (data) => {
  console.log ('got some data:', data);
});

In your helper module:

'use strict';

const EventEmitter = require('events');

module.exports.events = new EventEmitter();

// do things here...

module.exports.events.emit('finished', someAwesomeData);