No handler registered for channel - Electron IPC handle/invoke

Viewed 3940

In my electron app, I am receiving an error "No handler registered for 'channel-name' at EventEmitter../lib/renderer/api/ipc-renderer.ts.ipcRenderer.invoke (electron/js2c/renderer_init.js:1163:19)". IPC handle is not registered and the communication between renderer and main process is not working as expected in Windows.

When I open the app from windows installer, I see this issue and it's intermittent when open the app from the applications on windows.

Here is the code snippet of how I used ipc handle/invoke.

In Main.ts file

import {ipcMain} from 'electron';
private registerIpcChannels(ipcChannels: IpcChannelInterface[]) {
        try {
            for(const channel of ipcChannels) {
                ipcMain.handle(channel.getName(), async (event, request) => await channel.handleInvoke(event, request))
            }
        } catch (err) {
            log.error(`Unable to register IPC channels: ${err.message}`, err);
        }
    }

In Renderer.js file:

import electron from 'electron';
sendMessage(msg) {
        let ipc = electron.ipcRenderer;
        ipc.invoke(channelName, msg);
}

I don't understand that why it is working completely fine in MacOS but not on windows. I followed the documentation to implement handle/invoke in electron app. Environment info: Electron - v8.5.2 Nodejs - v12.16.1 OS - Windows 10

Note: This is happening only at the time of first-time launch. After refresh or quit and reopen the app, it works fine

Can anyone help to figure out why this issue is occurring?

1 Answers

I had this same issue. My problem was that I was instructing electron to navigate to my app before my IPC handlers were being registered. The reason it was only happening on the first load and not subsequent loads/reloads (and hot-reloads) was because the BrowserWindow was getting recreated and my IPC handlers were already registered (and getting re-registered in my code).

So be absolutely sure that your handlers are being registered before the BrowserWindow loads the renderer code that calls the IPC channels.

Related