ipcMain in multiple files



Is it possible to have the ipcMain in multiple files( I mean, other than main.js of electron can we use ipcMain in other files too). If that is possible, can you please guide me how to use it.

Ty in advance.


So far I know, ipcMain is a means for Electron to communicate from main with the renderer and the inverse.

Let me explain (and someone correct me if I am wrong): Electron has 2 types of processes: Main process and Renderer process. Main do the ‘main’ electron tasks, like initialization and such, while Renderer takes the role to control what is display to the users via the window that you are rendering.

So, because of that, you can only access to ipcMain inside of the main process. If you want to communicate with the main.js, the renderer process has the equivalent ipcRenderer.

The main process is normally tied to a file called “main.js” (the name may vary but the idea is that), but that file can require other files, so, if well you cannot use ipcMain in the renderer project, it is possible to use it in different files as much as they belong to main.js. However, if you try “share” the same file between main and renderer expecting to “share information” or use ipcMain, it wont work due the fact that electron will keep “2 different copies” in ram for each process, and the renderer will not be able to load ipcMain from it.

I hope it helps you


@ancode Thanks for guidence, it really gave me some usefull info.