3

I've been using Electrons sync and async RPC communications mechanism and can pass my data between the processes quite nicely. However, I now need to continuously send event data (a bit like a chat application) to the renderer process and update some text.

Is this possible within electron? I'm guessing I'll need to create some kind of listener in the renderer process.

user1513388
  • 7,165
  • 14
  • 69
  • 111

2 Answers2

1

You can use ipcMain and ipcRenderer.

In main process.

const {ipcMain} = require('electron')
ipcMain.on('asynchronous-message', (event, arg) => {
  event.sender.send('asynchronous-reply', 'example message...')
})

In renderer process (web page).

const {ipcRenderer} = require('electron')

ipcRenderer.on('asynchronous-reply', (event, arg) => {
  // arg contain your message (example message...)
})
ipcRenderer.send('asynchronous-message', 'example example send to main process')

you can also pass any object.

MaximeF
  • 4,913
  • 4
  • 37
  • 51
0

Looks like it does. e.g.

Main Process:

const ipc = require('electron').ipcMain

ipc.on('asynchronous-message', function (event, arg) {
event.sender.send('asynchronous-reply', 'pong')

function countdown( elementName, minutes, seconds )
{
var element, endTime, hours, mins, msLeft, time;

function twoDigits( n )
{
  return (n <= 9 ? "0" + n : n);
}

function updateTimer()
{
  msLeft = endTime - (+new Date);
  if ( msLeft < 1000 ) {
    //element.innerHTML = "countdown's over!";
    event.sender.send('asynchronous-reply', 'countdown is over')
  } else {
    time = new Date( msLeft );
    hours = time.getUTCHours();
    mins = time.getUTCMinutes();
    // element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );
    event.sender.send('asynchronous-reply', (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() ));
    setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
  }
}

// element = document.getElementById( elementName );
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
updateTimer();
}
countdown( "countdown", 1, 5 );
})

Renderer Process:

 const {ipcRenderer} = require('electron')

 ipcRenderer.on('asynchronous-reply', (event, arg) => {
 // arg contain your message (example message...)
 })
 ipcRenderer.send('asynchronous-message', 'example example send to main process')
user1513388
  • 7,165
  • 14
  • 69
  • 111