-
-
Notifications
You must be signed in to change notification settings - Fork 736
IpcMain
Communicate asynchronously from the main process to renderer processes.
The Electron.IpcMain API provides inter-process communication between the main process and renderer processes. It allows you to send messages, listen for events, and handle communication between different parts of your Electron application.
Listens to channel, when a new message arrives listener would be called with listener(event, args...).
Parameters:
-
channel- Channel name to listen on -
listener- Callback method to handle incoming messages
Send a message to the renderer process synchronously via channel. Note: Sending a synchronous message will block the whole renderer process.
Parameters:
-
channel- Channel name to listen on -
listener- Synchronous callback method
Adds a one time listener method for the event. This listener is invoked only the next time a message is sent to channel, after which it is removed.
Parameters:
-
channel- Channel name to listen on -
listener- Callback method to handle the message once
Removes all listeners of the specified channel.
Parameters:
-
channel- Channel name to remove listeners from
Send a message to the BrowserView renderer process asynchronously via channel.
Parameters:
-
browserView- Target browser view -
channel- Channel name to send on -
data- Arguments to send
Send a message to the renderer process asynchronously via channel.
Parameters:
-
browserWindow- Target browser window -
channel- Channel name to send on -
data- Arguments to send
// Listen for messages from renderer
await Electron.IpcMain.On("request-data", (args) =>
{
Console.WriteLine($"Received request: {args}");
// Process the request and send response
});
// Send response back to renderer
Electron.IpcMain.Send(mainWindow, "data-response", processedData);// Handle synchronous requests
Electron.IpcMain.OnSync("get-user-info", (request) =>
{
var userId = request.ToString();
var userInfo = GetUserInfo(userId);
return userInfo;
});// Handle initialization request once
Electron.IpcMain.Once("app-initialized", (args) =>
{
Console.WriteLine("App initialized, setting up...");
InitializeApplication();
});// Send complex data to renderer
var appData = new
{
Version = "1.0.0",
Features = new[] { "feature1", "feature2" },
Settings = new { Theme = "dark", Language = "en" }
};
Electron.IpcMain.Send(mainWindow, "app-config", appData);
// Listen for settings updates
await Electron.IpcMain.On("update-settings", (settings) =>
{
var config = JsonConvert.DeserializeObject<AppSettings>(settings.ToString());
ApplySettings(config);
});// Send message to specific window
var settingsWindow = await Electron.WindowManager.CreateWindowAsync();
Electron.IpcMain.Send(settingsWindow, "show-settings", currentSettings);
// Broadcast to all windows
foreach (var window in Electron.WindowManager.BrowserWindows)
{
Electron.IpcMain.Send(window, "notification", message);
}// Handle IPC errors gracefully
await Electron.IpcMain.On("risky-operation", async (args) =>
{
try
{
var result = await PerformRiskyOperation(args);
Electron.IpcMain.Send(mainWindow, "operation-success", result);
}
catch (Exception ex)
{
Electron.IpcMain.Send(mainWindow, "operation-error", ex.Message);
}
});// Use with custom host functionality
await Electron.IpcMain.On("execute-host-function", async (args) =>
{
var functionName = args.ToString();
var result = await Electron.HostHook.CallAsync<string>(functionName);
Electron.IpcMain.Send(mainWindow, "function-result", result);
});- Electron.HostHook - Execute custom JavaScript functions
- Electron.WindowManager - Target specific windows for communication
- Electron.WebContents - Send messages to web content
- Electron IPC Documentation - Official Electron IPC API
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.