Slide 1

Slide 1 text

ELECTRON PRO-TIPS™ @PAULCBETTS (GITHUB, TWITTER)

Slide 2

Slide 2 text

HI.

Slide 3

Slide 3 text

HERE'S A FEW THINGS I NOTICE PEOPLE DOING IN ELECTRON APPS (THAT MAKE USERS MAD)

Slide 4

Slide 4 text

MEMORY USAGE MATTERS

Slide 5

Slide 5 text

Users get so mad about memory usage. Which is mostly nonsense.

Slide 6

Slide 6 text

Every conversation I've ever had about Electron memory usage: Them: IM SO MAD ABOUT MEMORY USAGE Me: I understand! So, what's the "Commit Charge" say in Task Manager? That's the percentage of RAM that is actually in-use. Them: Oh, it's 40%.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

...but on the other hand, as Electron Developers, we've got great tools to do something about this, so we should!

Slide 10

Slide 10 text

LOAD LESS STUFF

Slide 11

Slide 11 text

LOAD LESS STUFF ▸ Lots and Lots of DOM Elements ▸ Especially Images and Long Scrollable Areas ▸ JS Heap Memory

Slide 12

Slide 12 text

USE REACT OR VUE (AND VIRUALIZING LISTS)

Slide 13

Slide 13 text

LOAD LESS STUFF ▸ Libraries that you load in your app never get unloaded ▸ Bad for startup performance and for memory usage!

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

USE THE HEAP PROFILER

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

DON'T RUN STUFF IN THE MAIN PROCESS

Slide 18

Slide 18 text

but what about...?

Slide 19

Slide 19 text

NO

Slide 20

Slide 20 text

WHAT THE MAIN PROCESS IS NOT ▸ "The Backend" ▸ "A Background Thread" ▸ "The Server"

Slide 21

Slide 21 text

THE MAIN PROCESS IS FOR ORCHESTRATION

Slide 22

Slide 22 text

THE MAIN PROCESS IS FOR ORCHESTRATION Running code in the main process slows everything else down Chromium uses IPC internally to do things, such as signaling window size changes So when the main thread is busy, your app glitches!

Slide 23

Slide 23 text

THE MAIN PROCESS IS FOR ORCHESTRATION ipc.send is asynchronous which is Better, but not enough! The main process can still do a lot of work as a result of ipc.send, and block stuff

Slide 24

Slide 24 text

THE MAIN PROCESS IS FOR ORCHESTRATION The main process should really only be used to tell other processes what to do ▸ Sending information between windows ▸ Signalling menu items and dock events ▸ Crash reporting and other APIs that only work in the main process

Slide 25

Slide 25 text

SO HOW CAN I DO STUFF THEN??

Slide 26

Slide 26 text

WHAT IF WE CREATE A BROWSERWINDOW BUT DIDN'T SHOW IT?

Slide 27

Slide 27 text

ELECTRON-REMOTE

Slide 28

Slide 28 text

ELECTRON-REMOTE, DOING WORK FROM THE MAIN PROCESS import { createProxyForRemote } from 'electron-remote'; // myWindowJs is now a proxy object // for myWindow's `window` global object const myWindowJs = createProxyForRemote(myWindow); // Functions suffixed with _get // will read a value userAgent = await myWindowJs.navigator.userAgent_get()

Slide 29

Slide 29 text

ELECTRON-REMOTE, TASKPOOL import { requireTaskPool } from 'electron-remote'; const myCoolModule = requireTaskPool( require.resolve('./my-cool-module')); // This method will run synchronously, // but in a background BrowserWindow process // so that your app will not block let result = await myCoolModule.calculateDigitsOfPi(100000);

Slide 30

Slide 30 text

REQUESTIDLECALLBACK IS SUPER COOL

Slide 31

Slide 31 text

REQUESTIDLECALLBACK IS SUPER COOL Like setTimeout but only runs once the UI is no longer busy The callback allows you to repeatedly schedule requestIdleCallback to do work in a loop Writing App Data in the background is a great place to use requestIdleCallback

Slide 32

Slide 32 text

JUST MAKE AN HTML PAGE

Slide 33

Slide 33 text

THIS EXPERIENCE IS A DRAG.

Slide 34

Slide 34 text

JUST MAKE AN HTML PAGE Putting a website into an Electron frame is easy, but not great for Users Offline Mode is way easier Your app will start Really Fast Starting with a Desktop Mindset will make your app feel like an app

Slide 35

Slide 35 text

HTML PAGES ARE MORE SECURE Designing a hybrid app is Very Security Sensitive, so that you don't accidentally give Desktop Powers™ to remote content When all of the code for your app is local, you remove this possibility altogether XSS is still extremely important to watch out for!

Slide 36

Slide 36 text

DON'T RUN WEB SERVERS IN YOUR APP

Slide 37

Slide 37 text

DON'T RUN WEB SERVERS IN YOUR APP ...cause like, what if more than one user uses your app? Your web service now a great way to move data between different users If you run as Admin, it's now a great way to local EoP ...or if you're really unlucky, have arbitrary websites run Desktop code

Slide 38

Slide 38 text

USE ELECTRON-FORGE

Slide 39

Slide 39 text

USE ELECTRON-FORGE electron-forge handles all of the things you might want to use Express or Webpack for, like Hot Module Reload It handles Babel/TypeScript/LESS/Sass via hooking Electron and compiling on-the-fly during development electron-forge does all of the packaging and compilation work too

Slide 40

Slide 40 text

BUT I LIKE WEBPACK!

Slide 41

Slide 41 text

BUT I LIKE WEBPACK! Trying to interact with Electron itself gets Weird because now there are two separate module systems Native node modules are a pain with Webpack, both at runtime and on the build side Packaging becomes way more complex

Slide 42

Slide 42 text

PERFORMANCE, BLAHHH

Slide 43

Slide 43 text

SECURITY, BORINGGGG

Slide 44

Slide 44 text

MEMORY, UGHHHHHH

Slide 45

Slide 45 text

NODE-RT IS COOL

Slide 46

Slide 46 text

CALL WIN10 APIS FROM ELECTRON SUPER EASILY

Slide 47

Slide 47 text

SOME COMPELLING EXAMPLES: - Windows.Devices.Display - Windows.Devices.Geolocation - Windows.Media.Capture - Windows.Media.OCR - Windows.System.Power

Slide 48

Slide 48 text

WHAT ABOUT MACOS?

Slide 49

Slide 49 text

¯\_(ϑ)_/¯

Slide 50

Slide 50 text

¯\_(ϑ)_/¯ There's no easy way to call macOS APIs from Electron, you have to write a Native Node Module. You can do very simple things with node-ffi, but more complicated things will lead to Segfault City

Slide 51

Slide 51 text

HOW CAN I FIGURE OUT WHAT I CAN DO?

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

SO IN SUMMARY

Slide 57

Slide 57 text

WRITE DESKTOP APPS, BY JUST MAKING AN HTML PAGE

Slide 58

Slide 58 text

AND LOAD LESS STUFF

Slide 59

Slide 59 text

AND LOAD IT IN THE RENDERER PROCESS

Slide 60

Slide 60 text

DO COOL DESKTOP STUFF (IN WINDOWS 10)

Slide 61

Slide 61 text

THANKS! @PAULCBETTS (GITHUB, TWITTER, HE/HIM)

Slide 62

Slide 62 text

No content