concept Electron in category electron

appears as: Electron, Electron, Electron, The Electron
Electron in Action

This is an excerpt from Manning's book Electron in Action.

For a long time, building for the desktop has involved adopting a completely different skill set. Many of us don’t have the bandwidth to take on the long learning curve necessary for learning new languages and frameworks. With Electron, you can use your existing skills as a web developer to build applications that have many of the capabilities of a native desktop application.

1.1. What is Electron?

Electron is a runtime that allows you to create desktop applications with HTML5, CSS, and JavaScript. It’s an open source project started by Cheng Zhao (aka zcbenz), an engineer at GitHub. Previously called Atom Shell, Electron is the foundation for Atom, a cross-platform text editor by GitHub built with web technologies.

You may have heard of—or used—Apache Cordova or Adobe PhoneGap for building web applications—wrapped in native shells—for mobile operating systems such as iOS, Android, and Windows Phone. If so, then it might be helpful to think of Electron as a similar tool for building desktop applications.

Electron allows you to use the web technologies you already know to build applications that you wouldn’t otherwise build. In this book, you’ll learn how to build applications that hook into native operating system APIs on Windows, macOS, and Linux.

Electron combines the Chromium Content Module and Node.js runtimes. It allows developers to build GUIs with web pages as well as access native operating system capabilities on Windows, macOS, and Linux through an OS-agnostic API.

Chromium and Node are both wildly popular application platforms in their own right, and both have been used independently to create ambitious applications. Electron brings the two platforms together to allow you to use JavaScript to build an entirely new class of application. Anything you can do in the browser, you can do with Electron. Anything you can do with Node, you can do with Electron.

The exciting part is what you can do with the two technologies together. You can build applications that take advantage of both platforms and build applications that wouldn’t otherwise be possible on only one. That’s what this book is all about. Electron is not only a great choice for building web applications that behave like native desktop applications; it’s also a great choice for building a GUI around Node applications that would otherwise be limited to a command-line interface. See figure 1.1.

Figure 1.1. LevelUI is a GUI for Node’s LevelUp database built with Electron. You couldn’t build this application in a traditional browser because it wouldn’t have the ability to access a local database on the user’s computer. It also couldn’t use the LevelUI library because it’s a compiled C++ module, which only Node—and not the browser— can use.

Let’s say that you want to build an application that allows you to view and edit a folder of images on your computer. Traditional browser applications can’t access the filesystem. They couldn’t access the directory of photographs, load any of the photographs in the directory, or save any of the changes that you made in the application. With Node, you could implement all those features, but you couldn’t provide a GUI, which would make your application difficult to use for the average user. By combining the browser environment with Node, you can use Electron to create an application where you can open and edit photographs as well as provide a UI for doing so. See figure 1.2.

Figure 1.2. Electron combines the core web browsing component of Chromium with the low-level system access of Node.

Electron isn’t a complicated framework—it’s a simple runtime. Similar to the way you might use node from the command line, you can run Electron applications using the electron command-line tool. You don’t have to learn many conventions to get started, and you’re free to structure your application however you’d like—although I’ll provide tips and best practices throughout this book.

Figure 1.1. LevelUI is a GUI for Node’s LevelUp database built with Electron. You couldn’t build this application in a traditional browser because it wouldn’t have the ability to access a local database on the user’s computer. It also couldn’t use the LevelUI library because it’s a compiled C++ module, which only Node—and not the browser— can use.
Listing 2.2. Adding a basic main process: ./app/main.js
const {app} = require('electron');

app.on('ready', () => {                   #1
  console.log('Hello from Electron');
});
Cross-Platform Desktop Applications: Using Node, Electron, and NW.js

This is an excerpt from Manning's book Cross-Platform Desktop Applications: Using Node, Electron, and NW.js.

In the Node.js ecosystem, there are two major frameworks for creating desktop apps: NW.js and Electron. Both are supported by major businesses (NW.js by Intel and Gnor Tech, and Electron by GitHub), both have large communities around them, and both share similar approaches to building desktop apps. This book shows how to build cross-platform desktop apps with both Electron and NW.js. You may be pleasantly surprised by how much they have in common—in fact, they have something of a shared history, but we’ll get to that later. For now, we’ll explore some of the reasons why Node.js desktop apps have taken off and where they might be useful for you and your work.

1.4. Introducing Electron

Electron is a desktop app framework from GitHub. It was built for GitHub’s text editor Atom and was originally known as Atom Shell. It allows you to build cross-platform desktop apps using HTML, CSS, and JavaScript. Since its release back in November 2013, it has become popular and is used by a number of startups and large businesses for their apps. Electron is used not only in Atom but also in the desktop clients of a chat app called Slack (www.slack.com), a startup that was valued at $3.8 billion as of April 2016.

Listing 1.2. main.js file for the Hello World Electron app
'use strict'; const electron = require('electron'); #1 const app = electron.app; #2 const BrowserWindow = electron.BrowserWindow; #3 let mainWindow = null; #4 app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); #5 }); app.on('ready', () => { #6 mainWindow = new BrowserWindow(); mainWindow.loadURL(`file://${__dirname}/index.html`); #7 mainWindow.on('closed', () => { mainWindow = null; }); #8 }); #1 - Loads Electron module from npm #2 - Creates reference to Electron’s application object #3 - Creates reference to Electron’s Browser-Window class #4 - mainWindow variable stores reference to app’s window #5 - Add event listener that quits the app when all windows are closed (except in Mac OS) #6 - Creates new app window and assigns it to main window variable to prevent it being closed by Node.js garbage collection #7 - Loads index.html file in app window #8 - When app window is closed, unassigns app window from main window variable
Figure 1.8. The Hello World example app running with Electron on Mac OS. It looks almost identical to the NW.js equivalent, except the window dimensions are different.