I’m using Electron for the front-end of my application. In the IcpMain, the main renderer, I create a socket instance. Now I want to use the same socket instance in the other Electron processes.
I tried sharing the socket via a send:
ipcMain.on("get-socket", function (event, arg) {
event.sender.send("socket", socket);
});
however, the socket is not serializable (using JSON doesn’t help as it is circular).
Then I tried creating a socketManager.js:
const io = require("socket.io-client");
let socketInstance;
function createSocket(server, port) {
if (!socketInstance) {
socketInstance = io(`ws://${server}:${port}`, {
transports: ["websocket"],
});
}
return socketInstance;
}
function getSocket() {
return socketInstance;
}
module.exports = { createSocket, getSocket,};
However, the socketInstance seems to be undefined after each import, even though I first initialize it in the main process and then call getSocket in the other Electron processes.
Does anyone have any tips?