Skip to content

IpcMain

github-actions[bot] edited this page Oct 15, 2025 · 2 revisions

Communicate asynchronously from the main process to renderer processes.

Overview

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.

Key Methods

Event Handling

On(string, Action)

Parameters:

  • channel - Channel name to listen on
  • listener - Callback method to handle incoming messages

Description: Listens to channel, when a new message arrives listener would be called with listener(event, args...).

OnSync(string, Func<object, object>)

Parameters:

  • channel - Channel name to listen on
  • listener - Synchronous callback method

Description: Send a message to the renderer process synchronously via channel. Note: Sending a synchronous message will block the whole renderer process.

Once(string, Action)

Parameters:

  • channel - Channel name to listen on
  • listener - Callback method to handle the message once

Description: 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.

RemoveAllListeners(string)

Parameters:

  • channel - Channel name to remove listeners from

Description: Removes all listeners of the specified channel.

Message Sending

Send(BrowserWindow, string, params object[])

Parameters:

  • browserWindow - Target browser window
  • channel - Channel name to send on
  • data - Arguments to send

Description: Send a message to the renderer process asynchronously via channel.

Send(BrowserView, string, params object[])

Parameters:

  • browserView - Target browser view
  • channel - Channel name to send on
  • data - Arguments to send

Description: Send a message to the BrowserView renderer process asynchronously via channel.

Usage Examples

Basic Message Handling

// 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);

Synchronous Communication

// Handle synchronous requests
Electron.IpcMain.OnSync("get-user-info", (request) =>
{
    var userId = request.ToString();
    var userInfo = GetUserInfo(userId);
    return userInfo;
});

One-time Event Handling

// Handle initialization request once
Electron.IpcMain.Once("app-initialized", (args) =>
{
    Console.WriteLine("App initialized, setting up...");
    InitializeApplication();
});

Complex Data Exchange

// 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);
});

Multi-Window Communication

// 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);
}

Error Handling

// 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);
    }
});

Integration with Host Hooks

// 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);
});

Related APIs

Additional Resources

Clone this wiki locally