0

I'm trying to do a very simple communication between ipcRenderer and IPCMain but it's not working ! can someone tell me why ?

GALLERY.JS

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


document.addEventListener('DOMContentLoaded', (e) => {
    ipcRenderer.send('test');
});

I really don't understand why nothing is printed in my console

GALERYCONTROLLER.JS

const { ipcMain} = require('electron');
const userId;
const Axios = require('axios')



ipcMain.on('test', (e) =>{
    console.log('droneDataGallery received')
   })

});

gallery.ejs

<link rel="stylesheet" href="../assets/css/GalleryPage.css"></link>

<div class='galleryPage'>

</div>

<script src="./../assets/js/gallery.js"></script>

Thanks a lot for your help !

  • We'd likely need more context. Is it possible that the message is being sent before the handler is registered? – pushkin Oct 11 '21 at 15:55

2 Answers2

0

Assuming you use the latest version of Electron, you need to expose the ipc messaging system to your HTML (gallery.js) code through the contextBrige object at the preload.js level. This is very well explained here and it worked great for me. Of course, it implies some extra plumbing.

0
document.addEventListener('DOMContentLoaded', (e) => {
    ipcRenderer.send(  'test'  ,data you want to send to GALERYCONTROLLER.JS);
});

ipcMain.on('test', (e, data) =>{
// data is array

    console.log(data)
   })

});