jbranchaud/til: Today I Learned

325 words , ,

Josh Branchaud wrote a small file detailing some bit of programming knowledge he learned every day for 328 days. I really like this idea and I think I might just do it. I certainly won’t be posting as often as he is – I don’t learn quickly enough – but it’d be nice to be able to look back on everything I’ve learned in one year.

So what did I learn today? 

Electron has two processes – a main and a renderer. Both have access to node.js but almost all of your code should sit in the renderer. This is not what I expected. I assumed my code would go in main.js, but apparently this is really only for very low level stuff like manipulating the window and menu. Even database / file access code belongs in the renderer!

Differences Between Main Process and Renderer Process

The main process creates web pages by creating BrowserWindow instances. Each BrowserWindow instance runs the web page in its own renderer process. When a BrowserWindow instance is destroyed, the corresponding renderer process is also terminated.

The main process manages all web pages and their corresponding renderer processes. Each renderer process is isolated and only cares about the web page running in it.

In web pages, calling native GUI related APIs is not allowed because managing native GUI resources in web pages is very dangerous and it is easy to leak resources. If you want to perform GUI operations in a web page, the renderer process of the web page must communicate with the main process to request that the main process perform those operations.

In Electron, we have provided the ipc module for communication between the main process and renderer process. There is also a remote module for RPC style communication.

Note: If I do start a series of TIL’s I’ll probably follow Josh’s example and stick it on github or somewhere. I’ll keep it out of the blog.