Pass function in IPC call


#1

Hello everyone

here is my object
{
Name:‘Test’,
getname:function(){ return ‘test’;}
}

I want to pass this object through IPC.

But At receiving side i am getting {Name:‘Test’} object only.
No methods.

Please help .

I want to pass complete object to webview.

Thank you.


#2

According to the docs on window.webContents.send, this is not possible (nor can it be done from the renderer):

Send an asynchronous message to renderer process via channel, you can also send arbitrary arguments. Arguments will be serialized in JSON internally and hence no functions or prototype chain will be included.

However, if you are trying to get the function from the main process to the renderer, you can store the function as a global in the main process and get it in the renderer:

// in main process
global.myTempFunction = function() {};

// in renderer
const { remote } = require('electron');
const myRemoteFunction = remote.getGlobal('myTempFunction');
// do whatever with your remoteFunction

However this is only for functions from the main process, to the renderer. As far as I know, it cannot be done the other way around.


#3

Is this true, it can not be done the other way around? The renderer can’t assign a function to the main process global?

What about getting renderer process globals (f.e. the window in webContents)? Can we get those? If we can get a reference to those, can we assign references onto them? If not, why not?