bringing javascript to the desktop with electron

29
© Copyright SELA Software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com SELA DEVELOPER PRACTICE JUNE 19-23, 2016 Nir Noy Consultant, Web, Sela @noynir Bringing Javascript to the Desktop with Electron

Upload: nir-noy

Post on 13-Feb-2017

280 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Bringing Javascript to the Desktop with Electron

© Copyright SELA Software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com

SELA DEVELOPER PRACTICEJUNE 19-23, 2016

Nir NoyConsultant, Web, Sela@noynir

Bringing Javascript to the Desktop with Electron

Page 2: Bringing Javascript to the Desktop with Electron

Why Desktop?OfflineLocal hardware(printers, devices etc…)SecurityOn-premiseApp storeLocal AccessKioskIt Just ”Feels Right”

Page 3: Bringing Javascript to the Desktop with Electron

Desktop AppsDevelopment ToolsTasksMedia playersEmail clientCalendarTime managementMappingSocial media clientsFile management

Backup

Audio and Video ConferencingGaming

Page 4: Bringing Javascript to the Desktop with Electron

What is ElectronA framework for building cross platform Desktop Applications using web technologies

Page 5: Bringing Javascript to the Desktop with Electron

What is ElectronCreated by GitHub as the shell for the Atom text editor.

Page 6: Bringing Javascript to the Desktop with Electron

Road to Electron

Chromium Embedded Framework

NW.JS

Page 7: Bringing Javascript to the Desktop with Electron

What’s Under the Hood

+ =NodeJS Chromium

Page 8: Bringing Javascript to the Desktop with Electron

Cool StuffWeb Technologies – No Native Skills needed

Javascript, HTML, CSS only.One Browser

Chrome compatible onlyLatest Features – ES 2015 & Chrome goodies.

Built-In NodeJSAll Node Capabilities - File system, networking etc..npm ecosystem

Cross platformPackaging for Windows, OS X, Linux

Native UXNative Dialogs, Menus, Notifications etc…

Page 9: Bringing Javascript to the Desktop with Electron

Uncool StuffWeb Technologies

Seriously it’s Javascript, HTML and CSS all the way.Native Modules

Native Modules are written in C/C++ .Debugging

Certain parts of the application are hard to debug.Tooling

Some of the tooling is immature.

Page 10: Bringing Javascript to the Desktop with Electron

How Does it Work?

A Tale Of Two processes

Page 11: Bringing Javascript to the Desktop with Electron

A Tale Of Two processes

Main Application lifecycle

Browser window ipc

Node.js menu dialog

Renderer Web page

ipc webFrame

Node.jsDOM remote

Creates

Page 12: Bringing Javascript to the Desktop with Electron

A Tale Of Two processes

Main Application lifecycle

Browser window ipc

Node.js menu dialog

Renderer Web page

ipc webFrame

Node.jsDOM remote

Creates

Page 13: Bringing Javascript to the Desktop with Electron

A Tale Of Two processes

Main Application lifecycle

Renderer Web page

Renderer Web page

Renderer Background Worker

Page 14: Bringing Javascript to the Desktop with Electron

Getting StartedHello Electron App

Page 15: Bringing Javascript to the Desktop with Electron

Getting StartedCreate a main.js file

...app.on('ready',() => {

mainWindow=new BrowserWindow({width:800, height:600}); mainWindow.loadURL('file://'+ __dirname +'/index.html'); mainWindow.on('closed',function(){

mainWindow=null; });

});app.on('window-all-closed',() => {

if (process.platform != 'darwin') { app.quit(); }});

Page 16: Bringing Javascript to the Desktop with Electron

Getting StartedCreate an index.html file

...<body> <h1>Hello Electron!</h1> We are using node <script> var dv=document.createElement('div'); dv.innerHTML=`We are using node ${process.versions.node}

<br/>Chrome ${process.versions.chrome}<br/> Electron ${process.versions.electron}`; document.body.appendChild(dv); </script>

</body>...

Page 17: Bringing Javascript to the Desktop with Electron

Project SetupCreate a Package.json file

{"name" : "your-app", "version" : "0.1.0", "main" : "main.js"

}

Page 18: Bringing Javascript to the Desktop with Electron

Running Your AppInstall Electron prebuilt binaries using npm.

$ npm install electron-prebuilt --save-dev // -g

Run your app with the Electron Command$ electron your-app/

Page 19: Bringing Javascript to the Desktop with Electron

Demo

Hello Electron

Page 20: Bringing Javascript to the Desktop with Electron

Processes CommunicationThe Main and Renderer Processes can communicate using their ipc modules.Each type of processes has it’s own ipc module.In the Renderer process use the ipcRenderer module to send and listen to messages

const ipcRenderer = require('electron').ipcRenderer;

ipcRenderer.on('asynchronous-reply', function(event, arg) {

console.log(arg); // prints "pong"

});

ipcRenderer.send('asynchronous-message', 'ping');

Page 21: Bringing Javascript to the Desktop with Electron

Processes CommunicationIn the Main process use the ipcMain module to listen and reply to messages.

const ipcMain = require('electron').ipcMain;

ipcMain.on('asynchronous-message', function(event, arg)

{ console.log(arg); // prints "ping"

event.sender.send('asynchronous-reply', 'pong');

});

Page 22: Bringing Javascript to the Desktop with Electron

Processes CommunicationThe remote module provides a simple way to do inter-process communication (IPC) between the renderer process (web page) and the main process.

It allow method invocation 0f main process objects without explicitly using the ipc modules.

const remote = require('electron').remote;

const BrowserWindow = remote.BrowserWindow;

var win = new BrowserWindow({ width: 800, height: 600 });

win.loadURL('https://github.com');

Page 23: Bringing Javascript to the Desktop with Electron

Demo

Advanced Demo

Page 24: Bringing Javascript to the Desktop with Electron

Packaging and Distributing Install Electron packager using npm.

$ npm install electron-packager --save-dev / -g

Run your app with the Electron Command$ electron-packager app-name --platform=win32 --arch=x64

Page 25: Bringing Javascript to the Desktop with Electron

Built With ElectronVisual Studio Code

Atom

SlackWhat’s App

Page 26: Bringing Javascript to the Desktop with Electron

Electron ToolsElectron API Demos app

Electron App for interactively exploring the Electron API.Devtron

Electron Extension for Chrome’s Devtools.Allow inspection and monitoring of your app.

SpectronFramework for writing integrations tests.Built on top of ChromeDriver and WebDriverIO.

Page 27: Bringing Javascript to the Desktop with Electron

Community Electron Tools & Resourceselectron-builder - Create installers.electron-boilerplate generator-electron - yeoman generator

Awesome electronhttps://github.com/sindresorhus/awesome-electron

Page 28: Bringing Javascript to the Desktop with Electron

Questions