Learning Electron: 4 Things I Wish I Knew Sooner
Although the community surrounding Electron makes it easy to get started, there are a lot of mistakes people frequently make and things people misunderstand that chew up a lot of their time. So, here are a few tips to make your experience faster and easier.
Join the DZone community and get the full member experience.Join For Free
electron is a fun stack to work with. if you’re familiar with node or with building front ends in the browser, electron makes it easy to build desktop apps.
although the community surrounding electron makes it easy to get started, there are a lot of mistakes people frequently make and things people misunderstand that chew up a lot of their time.
for the sake of brevity here, i’ll assume that you’ve gone/will go through the electron getting started docs.
4 things i've learned that might help you.
1. keep your code separated
object, your application will not fare so well.
"i’ll just be careful," you might say... developers are, after all, masters of hubris above all else. it’s well worth it to spend 20 minutes up front avoiding hours of refactoring later. i generally find it worthwhile to separate code into a few folders: one for the main process, one for each type of window, and one for shared code that should get run across all types of processes.
it looks like this:
background: a hidden window (more on these later)
main : main process
renderer : the window the user will interact with
shared : code that gets loaded for both the main process and all windows
2. accommodate for native modules
the prevalent workflow for this is to install normally, and then run a grunt task or similar script to compile your modules again to work with electron. my best guess is that people prefer compiling twice so that they have more time for swordfighting . however, while i enjoy swordfighting, i also have entirely too much to get done, so i prefer to only compile code once.
to add to it, while most modules will compile in 30 seconds or so, some larger modules like nodegit can take nearly 10 minutes to compile, so compiling twice is a brutal time waster. mentioned very briefly in the electron docs is an alternative, which is the way i prefer to do things. code is only compiled once, plus this way we can take advantage of modules that provide pre-compiled binaries. using a file called a ".npmrc" in the root of our project, we can get npm to compile/download everything correctly from the get go.
below, you'll see a .npmrc setup for using electron 0.36.5.
the other thing to pay attention to here is that native modules compiled for electron won’t necessarily work normally in node. so if you have a grunt build process or anything of the sort which uses some native modules, they’ll need to be kept separately.
what i do (and what they do in atom) is to separate out modules into two areas to distinguish between those used in the application, and any modules used in the build but outside of the actual application. i keep those used in the application (including actual dependencies, and dev dependencies like test frameworks that run inside of the application) in the root of my project, and a build folder with its own package.json file for those modules used outside the application:
3. don’t block the ui thread or the main thread
node is single threaded. while electron runs multiple processes, each one is still single threaded. so, if you run a long running synchronous task in your window, the user isn’t going to be able to interact with your application for the duration of its task. unless you’re out to spite the end user, this is generally regarded as "bad."
the common first attempt at getting around this that new-to-electron developers make is to move things to the main process, but this is still "bad." lots of small actions (like quitting the app, resizing the window, etc.) happen in the main process, so now, while your buttons are still clickable, the app can’t resize, or exit, or any number of other related things which make the app seem frozen and broken to the end user.
in the browser, people generally get around this by using web workers, but rather than spend time implementing that, the electron team decided to make it even easier. to perform processes that don’t block the main process or renderer process, all you have to do is open another window that isn’t visible to the user.
this invisible window will open up the other process behind the scenes. despite being invisible to the user, it can communicate with the main process (and other windows) using electron’s ipc module just like a normal viewable window.
4. use old school for web requests
if your app needs to do any communication over the web (perhaps to an api server or such), it’s logical to use the built-in node http/https modules, or the popular "request" module. however, none of these accommodate for system-configured proxies.
chromium, however, does account for this, and fortunately, it’s super easy to tap into that, just by using the old-school browser xmlhttprequest object. by using xmlhttprequest, it’s all handled automatically, and you don’t have to do any extra work or make the user configure anything in your application.
take advantage of my suffering
i hope you’ll take a real stab at using electron. i spent plenty of my supposed vacation time on the beach still working, but maybe these tips mean that next time you go on vacation you can actually do some vacationing.
Published at DZone with permission of Max Korp. See the original article here.
Opinions expressed by DZone contributors are their own.