How to set global variable of main process


#1

remote.getGlobal(name) will give the name variable from main process, but how to set a variable in main process ?


#2

this works for me

in main process:

global.sharedObj = {prop1: null};

in renderer process:

remote.getGlobal('sharedObj').prop1 = 125;

#3

This doesn’t work for me…

In my main process main.js

global.sharedObj = {prop1: null};

And I try to access that on console like this:
require(‘electron’).remote.getGlobal(‘sharedObj’)

require(‘electron’).remote.getGlobal(‘sharedObj’).prop1

I’m using electron 0.36.0, can you please share me your source code?


#4

ok, my code (tested with electron 0.36.0):

main.js

var app = require('electron').app;
var BrowserWindow = require('electron').BrowserWindow;
var ipcMain = require('electron').ipcMain;

global.sharedObj = {prop1: null};

ipcMain.on('show-prop1', function(event) {
  console.log(global.sharedObj.prop1);
});

var mainWindow = null;

app.on('window-all-closed', function() {
  app.quit();
});

app.on('ready', function(){
  mainWindow = new BrowserWindow({width: 800, height: 600});  
  mainWindow.setMenu(null);     
  mainWindow.loadURL('file://' + __dirname + '/index.html');
  mainWindow.openDevTools();
  mainWindow.on('closed', function() {mainWindow = null;});
});

index.html

<!DOCTYPE html>
<html>
  <head>
  <script>
     var remote = require('electron').remote;     
     console.log(remote.getGlobal('sharedObj').prop1);     

     remote.getGlobal('sharedObj').prop1 = 125;     

     var ipcRenderer = require('electron').ipcRenderer;     
     ipcRenderer.send('show-prop1');
  </script>
  </head>
  <body>    
    <h1>Hello World!</h1>
    We are using io.js <script>document.write(process.version)</script>
    and Electron <script>document.write(process.versions['electron'])</script>.
  </body>
</html>

#5

I dunno anymore, this just not working


#6

That’s correct, value ‘125’ of prop1:

ipcMain.on('show-prop1', function(event) {
  console.log(global.sharedObj.prop1);
});

is not shown in the dev console, but in stdout (e.g. if you run app from commandline)


#7

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)


#8

So how to show it in dev console…?
It can be showed right?
I know it off-topic maybe… but assuming that the set global works then it should be obtainable from remote.getGlobal('sharedObj').prop1


#9
<script>
     var remote = require('electron').remote;     

     // show initial value from main process (in dev console)
     console.log(remote.getGlobal('sharedObj').prop1);     

     // change value of global prop1
     remote.getGlobal('sharedObj').prop1 = 125;     

     // show changed value in main process (in stdout, as a proof it was changed)
     var ipcRenderer = require('electron').ipcRenderer;     
     ipcRenderer.send('show-prop1');                       

     // show changed value in renderer process (in dev console)
     console.log(remote.getGlobal('sharedObj').prop1);
</script>

#10

it’s w-o-r-k-i-n-g!!!
thanks :grinning:


#11

Just an FYI I found that using remote.getGlobal just fine for reading global variables but problematic for assigning values to global variables on the Main process.

Using something like remote.getGlobal('sharedObj').prop1 = 125; as shown by @xdaks only changes the value of that global for that Renderer process. This is because remote copies the object instead providing a reference, as you can see in the documentation here:

Instead you need to use ipcMain and ipcRenderer to set the value fo the global variable properly:

main.js

const { ipcMain } = require( "electron" );

ipcMain.on( "setMyGlobalVariable", ( event, myGlobalVariable ) => {
  global.myGlobalVariable = myGlobalVariable;
} );

renderer.js

const { ipcRenderer, remote } = require( "electron" );

// Set MyGlobalVariable.
ipcRenderer.send( "setMyGlobalVariable", "Hi There!" );

// Read MyGlobalVariable.
remote.getGlobal( "MyGlobalVariable" ); // => "Hi There!"

See more here: