Ground Control Station Inter-Process Communication This page describes step by step how to setup IPC between main and renderer processes
Preload Scripts
Preload Scripts contain code that is executed in the renderer process. These scripts run in renderer context but have more privileges by having access to Node.js APIs, allowing them to have access to the OS functions.
Lets start by adding a Preload Script to our application
Create a Preload Script
In the main directory create preload.mjs
Copy import { contextBridge, ipcRenderer } from "electron";
// Exposing methods using contextBridge
contextBridge.exposeInMainWorld("mavlink", {
});
Import Preload Script in main
Copy import { BrowserWindow, app, ipcMain } from "electron";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let win;
const createWindow = () => {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.mjs"),
contextIsolation: true,
nodeIntegration: true,
},
});
if (process.env.NODE_ENV === "development") {
win.loadURL("http://localhost:5173");
} else {
win.loadFile(path.join(__dirname, "../../build/renderer/index.html"));
}
};
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
Using Inter-Process Communication
Renderer to Main (One-Way)
Listen for event on Main
Copy import { app, BrowserWindow, ipcMain } from "electron";
const handler = (event,data) => {
// Callback function to handle data
}
app.whenReady().then()(() => {
ipcMain.on("foo", handler);
createWindow();
})
Expose ipcRenderer.send
via Preload
Copy import { contextBridge, ipcRenderer } from "electron";
// Exposing methods using contextBridge
contextBridge.exposeInMainWorld("electronAPI", {
bar: (data) => ipcRenderer.send("foo",data);
});
Call the function in Renderer
Copy const buttonHandler = () => {
window.electronAPI.bar(data);
}
Renderer to Main (Two-Way)
Listen for events with ipcMain.handle
Copy import { app, BrowserWindow, dialog, ipcMain } from "electron";
const handler = async () => {
return data;
}
app.whenReady().then(() => {
ipcMain.handle("requestData", handler);
createWindow();
})
Expose ipcRenderer.invoke
via Preload
Copy import { contextBridge, ipcRenderer } from "electron";
// Exposing methods using contextBridge
contextBridge.exposeInMainWorld("electronAPI", {
request: () => ipcRenderer.invoke("requestData");
});
Request data from Renderer
Copy const buttonHandler = () => {
data = await window.electronAPI.request();
}
Main to Renderer (One-Way)
Send message from Main
Copy import { app, BrowserWindow, ipcMain } from "electron"
const senderFunction = () =>{
win.webContnets.send()
}
Expose ipcRenderer.on
via Preload
Copy const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
onData: (callback) => ipcRenderer.on('update-Data', (event, value) => callback(value))
})
Handle Data request in Renderer
Copy window.electronAPI.onData((value) =>{
//handle value here
})
Last updated 4 months ago