javascriptelectronipc

Send response to ipcRenderer from ipcMain


I am currently building an Electron app where I need to store something in the app. For that, I am using electron-store which works perfectly. Now I need to initialize an ipcMain.on("get-license-key"). Then I am calling ipcRenderer.send("get-license-key", "") now I want the ipcMain.on("get-license-key") to return the license key from electron-store so I can use it like this:

const licenseKey = ipcRenderer.send("get-license-key", "");

Solution

  • What you want to use is invoke/handle, and not send/on.

    renderer.js

    const licenseKey = await ipcRenderer.invoke("get-license-key");
    

    main.js

    ipcMain.handle("get-license-key", () => "the-licence-key");