electron ipc from main to renderer 1

electron ipc from main to renderer

// 4. The main process sends messages to the rendering process proactively

// The main process sends a message to the rendering process.

    // ipcMain.js
    const {BrowserWindow} = require("electron");

    BrowserWindow.getFocusedWindow().webContents.send(
        "rendererMsg",
        {msg:"Messages proactively sent by the main process to the rendering process"}
    )                    

// The rendering process listens for messages sent by the host process.

    // ipcRenderer.js
    const {ipcRenderer} =require("electron");

    window.onload = () => {
        // Listen for messages sent by the host process on its own initiative
        ipcRenderer.on("rendererMsg",(e,data)=>{
            console.log(data);
            // {'msg':'Messages proactively sent by the host process to the rendering process'}
        });
    }

Here is what the above code is Doing:
1. The main process sends a message to the rendering process.
2. The rendering process listens for messages sent by the host process.

Similar Posts