I have to get data from a server in my electron app I am using "net" module of the node js. As "net" is main process moduel I can't directly access it from renderer file. I had to use 'net' module in main.js file. I followed two way IPC from tutorial. With this approach I managed to fetch the data from server in main.js file as I can print in console log. But when I try to return this data to renderer it is not working. On renderer getting below messages
Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: undefined
const {
app,
BrowserWindow,
ipcMain,
net
} = require('electron');
const path = require("path");
const remoteMain = require('@electron/remote/main');
remoteMain.initialize();
let text;
let win;
function handleData() {
const request = net.request({
method: 'GET',
protocol: 'http:',
hostname: 'some IP address',
port: some port,
path: '/some path',
redirect: 'follow'
});
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
response.on('data', (chunk) => {
// console.log(`${chunk}`)
text = `${chunk}`;
console.log(text); // this prints the json data on console.
/ tried return text; but same error.
})
response.on('end', () => {
console.log('No more data in response.')
console.log(text); // this prints the json data on console.
return text;
})
})
request.end()
};
const createWindow = () => {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
})
remoteMain.enable(win.webContents)
}
function openIndex() {
win.loadFile('HTML/index.html');
win.webContents.openDevTools();
}
ipcMain.handle("request:get", handleData);
app.whenReady().then(() => {
createWindow();
openIndex();
app.on("activate", function() {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", function() {
if (process.platform !== "darwin") app.quit();
});
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
getData: () => ipcRenderer.invoke("request:get", message),
});
const btnclick = document.getElementById("PFLIoT");
btnclick.addEventListener("click", () => {
console.log("Hello from Machine List IOT");
const response = window.electronAPI.getData();
console.log(response);
// Also tried this code
// window.electronAPI.getData().then((data) => {
// console.log(data);})
});
<html>
<head>
<link rel="stylesheet" href="../css/layout.css" >
</head>
<body>
<div class="area"></div>
<nav class="main-menu">
<ul>
<li>
<a href="#">
<i class="fa fa-home fa-2x"></i>
<span class="nav-text">
Dashboard
</span>
</a>
</li>
<li class="has-subnav">
<a href="#home">
<i class="fa fa-laptop fa-2x"></i>
<span class="nav-text">
</span>
</a>
</li>
</nav>
</body>
<script src="../js/layout.js"> </script>
</html>
I finally got the answer. I just had to add one more line in preload.js file
ipcRenderer.on("returnResponse",(event,text) => {
console.log(text);
})