I am looking to send data from renderer.js to main.js and after certain operation return some values back to renderer.js.
const handleFilter = async () => {
const response=await window.electronAPI.fetchIssueAnalyses(data);
console.log(response);
};
const createWindow = () => {
//Connect to MYSQL server
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database: ', err);
} else {
console.log('Connected to MySQL database!');
}
});
ipcMain.on('set-testing', (event, data) => {
console.log(data)
})
ipcMain.on('fetch-issue-analyses', (event, data) => {
const { sdate, edate, cell } = data;
--- some operation -----
return value;
});
});
2
Answers
you can use the Electron’s inter-process communication (IPC) mechanism.
in renderer.js
In main.js
You can use ipcRenderer.invoke() and ipcMain.handle() for exactly that:
renderer:
main:
if you are using a preload script for higher security, you may wanna follow the official docs example:
https://www.electronjs.org/docs/latest/tutorial/ipc#1-listen-for-events-with-ipcmainhandle
https://www.electronjs.org/docs/latest/tutorial/tutorial-preload#communicating-between-processes