Eugen
Eugen

Reputation: 2990

Share ipc object between child and parent processes

I have an Electron application where I fork a child process to do some background job without disturbing the main process.

var onlineSyncChild = require('child_process').fork('./includes/server/onlineSync');

I'd like to send the ipcMain object to the the child process so it can listen to application messages and respond accordingly. Here is what I have

const electron = require('electron');
const app = electron.app;
const ipcMain = electron.ipcMain;

...

var onlineSyncChild = require('child_process').fork('./includes/server/onlineSync');
onlineSyncChild.on('message', function (m) {
  console.log('onlineSync says: ', m);
});
// send the ipcMain object into child
onlineSyncChild.send({type: 'set', ipc: ipcMain, db: DB});

However when I try to use it inside child process,

this.ipc.on('query-online-status', this.ipcQueryOnlineStatus);

I'm getting an error that this.ipc.on is not a function.

Am I correct to assume that this not possible, and all I can do send regular object only between child and parent processes?

Upvotes: 0

Views: 699

Answers (1)

Vadim Macagon
Vadim Macagon

Reputation: 14847

The message you pass to onlineSyncChild.send() will be serialized to a JSON string before being sent to the child process so any functions in the message will be omitted. You can read more about what's omitted during the serialization process in the documentation of JSON.stringify().

Upvotes: 1

Related Questions