Desktop apps with node webkit

99
DESKTOP APPS WITH NODE WEBKIT

description

This is a talk I gave at LNUG June 2014 about building desktop applications with Node Webkit

Transcript of Desktop apps with node webkit

Page 1: Desktop apps with node webkit

DESKTOP APPS WITH NODE WEBKIT

Page 2: Desktop apps with node webkit

What is Node Webkit?

Page 3: Desktop apps with node webkit

Node Webkit is an app runtime that lets you make desktop apps with HTML, CSS, and JavaScript.

Page 4: Desktop apps with node webkit

and it also lets you use Node.js modules within the

app as well.

Page 5: Desktop apps with node webkit

But not only that, you can build native executables of your app

for Windows, Mac and Linux

Page 6: Desktop apps with node webkit

Here are some examples using Node Webkit

Page 7: Desktop apps with node webkit

Light Tablelighttable.com

Page 8: Desktop apps with node webkit

Game Dev Tycoongreenheartgames.com/app/game-dev-tycoon

Page 9: Desktop apps with node webkit

Sqwigglesqwiggle.com

Page 10: Desktop apps with node webkit

How does it work?

Page 11: Desktop apps with node webkit

Node Webkit combines

Chromium Browser

Node.js Framework

Page 12: Desktop apps with node webkit

Integrating them requires the following:

• Using the same V8 engine instance

• Main loop integration

• Context Bridging

Page 13: Desktop apps with node webkit

Use the same V8 engine instance

• Both Chromium and Node use the V8 engine instance (Node use’s Chromium’s instance)

• The objects of Node and Chromium are in separate contexts, in order to keep the namespace clean.

Page 14: Desktop apps with node webkit

Main loop integration• The author says that it would take “some efforts” to

merge both Node and Chromium’s main loops.

• Node uses libuv for the events loop, but Chromium uses MessageLoop and MessagePump for its events loop.

• The author integrates these at the browser render process level, by making Chromium use a custom version of the MessagePump class, built on top of libuv

Page 15: Desktop apps with node webkit

Context Bridging• It’s a tricky but important part of integrating Node

into Chromium

• The first step is to initialise Node’s context first

• Next, once Webkit’s DOM context is installed, Node’s context is moved into Webkit’s context

• After that, Node’s start function is split into parts so that it can run within the context of Webkit’s render process

Page 16: Desktop apps with node webkit

For more information

• See this link:

• https://github.com/rogerwang/node-webkit/wiki/How-node.js-is-integrated-with-chromium

Page 17: Desktop apps with node webkit

Building a simple Node Webkit app

Page 18: Desktop apps with node webkit

Download Node Webkitgithub.com/rogerwang/node-webkit

Page 19: Desktop apps with node webkit

Create an app folder and some files

Page 20: Desktop apps with node webkit

Write a basic HTML page

Page 21: Desktop apps with node webkit

and the package.json file

Page 22: Desktop apps with node webkit

Execute the app

Page 23: Desktop apps with node webkit

and voila!

Page 24: Desktop apps with node webkit

Nice and easy, but pretty basic to be honest!

Page 25: Desktop apps with node webkit

Let’s try displaying the contents of my Home Folder

with Node.js

Page 26: Desktop apps with node webkit
Page 27: Desktop apps with node webkit
Page 28: Desktop apps with node webkit
Page 29: Desktop apps with node webkit

You can use not only Node’s core modules, but also npm

installed modules too!

Page 30: Desktop apps with node webkit

And we could iterate on this example further…

• We could turn the list items into links for exploring folders

• We could use front-end libraries like jQuery-ui or Bootstrap to improve the UI and make it display better

• We could make the file names (and possibly even contents) searchable using Lunr.js or an embedded database.

• We could stream those files to a server for file synchronisation (build your own Dropbox!)

Page 31: Desktop apps with node webkit

Window Rendering

Page 32: Desktop apps with node webkit

Window Rendering

• Node Webkit allows you to control how you want the Window rendering to work

• You can specify set dimensions, whether full-screen is allowed, and even if there’s a window frame at all!

Page 33: Desktop apps with node webkit

Dimensions

Page 34: Desktop apps with node webkit

Toolbar

Page 35: Desktop apps with node webkit

FullScreen

Page 36: Desktop apps with node webkit

Frameless

Page 37: Desktop apps with node webkit

Kiosk Mode

Page 38: Desktop apps with node webkit

Kiosk Mode• Useful for creating fullscreen applications that run in

public places (Banks, Retail, Venues)

• Access to the operating system is disabled on Mac, but you can gain access on Windows via Ctrl-Alt-Del keys (otherwise it’s blocked by AV Software as it looks like a virus to them)

• Its dangerous - if you don’t implement the javascript call to leave kiosk mode anywhere in your app, you’ll have to reboot your computer to regain access

Page 39: Desktop apps with node webkit

Window rendering can also be controlled programmatically

within the app

Page 40: Desktop apps with node webkit
Page 41: Desktop apps with node webkit

Menus

Page 42: Desktop apps with node webkit

You can add native menu items to your app, both in the main

menu, and in the app area

Page 43: Desktop apps with node webkit

Add the items via JSSource: code.tutsplus.com/tutorials/introduction-to-html5-

desktop-apps-with-node-webkit--net-36296

Page 44: Desktop apps with node webkit

And the menu appearsSource: code.tutsplus.com/tutorials/introduction-to-html5-

desktop-apps-with-node-webkit--net-36296

Page 45: Desktop apps with node webkit
Page 46: Desktop apps with node webkit

Tray Apps

Page 47: Desktop apps with node webkit

github.com/rogerwang/node-webkit/wiki/Tray

Page 48: Desktop apps with node webkit

Gulpgithub.com/sindresorhus/gulp-app

Page 49: Desktop apps with node webkit

Shell

Page 50: Desktop apps with node webkit

github.com/rogerwang/node-webkit/wiki/Shell

Page 51: Desktop apps with node webkit

Clipboard

Page 52: Desktop apps with node webkit

github.com/rogerwang/node-webkit/wiki/Clipboard

Page 53: Desktop apps with node webkit

Desktop integration is pretty good

Page 54: Desktop apps with node webkit

And because it’s using Chromium, HTML5 APIs

are available

Page 55: Desktop apps with node webkit

HTML5 APIs• Canvas (drawing, games)

• WebGL (3D, games)

• WebRTC (video/audio chat, P2P)

• CSS3 (games, animations)

• LocalStorage (offline editing)

• Audio/Video (video players, music streaming)

Page 56: Desktop apps with node webkit

So many possibilities!

Page 57: Desktop apps with node webkit

Soundnode (SoundCloud desktop app)

soundnodeapp.com

Page 58: Desktop apps with node webkit

Vocabulary Buildervocabularybuilder.co

Page 59: Desktop apps with node webkit

Fleex Playerfleex.tv

Page 60: Desktop apps with node webkit

But do remember, you’re working with static HTML

files

Page 61: Desktop apps with node webkit

You ‘could’ run a web server in your app, but…

Page 62: Desktop apps with node webkit

Packaging your app

Page 63: Desktop apps with node webkit

You can write the app once, and build it for Windows,

Mac and Linux

Page 64: Desktop apps with node webkit

Nuwk!codeb.it/nuwk

Page 65: Desktop apps with node webkit

grunt-node-webkit-buildernpmjs.org/package/grunt-node-webkit-builder

Page 66: Desktop apps with node webkit

Customer Case Study: The British Medical Journal

Page 67: Desktop apps with node webkit

Every year, we run the web site for a health conference

run by the BMJ and IHI

Page 68: Desktop apps with node webkit

As part of our service, we also produce a desktop app that lets people view recorded sessions, posters, and file attachments.

Page 69: Desktop apps with node webkit

Our desktop app was originally built on the

Adobe Air platform

Page 70: Desktop apps with node webkit

It had some disadvantages

Page 71: Desktop apps with node webkit

Disadvantages

• To use it, the person would have to have Flash installed on their computer

• Once they had installed the app onto the computer, they would then be prompted to locate the application’s content on the USB stick.

• Adobe Air is a declining platform (Linux no longer supported, Flash is dying)

Page 72: Desktop apps with node webkit

Last year, we decided to consider using Node Webkit to replace the Adobe Air app

Page 73: Desktop apps with node webkit

A couple of weeks after the event, we shipped the desktop

app, using Node Webkit

Page 74: Desktop apps with node webkit

The app’s tech stack• Node Webkit, with ffmpeg dependency added in for video

• Bootstrap 3 for the UI

• jQuery and Backbone for the front-end app

• Lunr.js for the search functionality

• Stylus, and Jade for creating the HTML and CSS files

• Hogan.js for templating

• Gulp for the building of the app’s files

Page 75: Desktop apps with node webkit
Page 76: Desktop apps with node webkit
Page 77: Desktop apps with node webkit
Page 78: Desktop apps with node webkit
Page 79: Desktop apps with node webkit

Benefits

• Users no longer had to install Flash or select the USB from a Folder Dialog. A simpler, better user experience.

• We were able to reuse some of the code from the web application in the desktop application.

• It allowed us to standardise around a Node tech stack within the company more.

Page 80: Desktop apps with node webkit

Key lessons

Page 81: Desktop apps with node webkit

HTML5 Video Fullscreen does not work!

Page 82: Desktop apps with node webkit
Page 83: Desktop apps with node webkit

We managed to get around this with a workaround

involving 3 steps:

Page 84: Desktop apps with node webkit

1 - Listen to the click event on the fullscreen button in

the video player

Page 85: Desktop apps with node webkit

2 - Add a CSS class to the video tag, which has properties that make the element take the full dimensions of

the window

Page 86: Desktop apps with node webkit

3 - Trigger the win.enterFullScreen() function call as well.

Page 87: Desktop apps with node webkit

Make use of Node Webkit’s dev tools

Page 88: Desktop apps with node webkit
Page 89: Desktop apps with node webkit

The dev tools will help to spot slow points in front-end rendering and performance

Page 90: Desktop apps with node webkit

Node Webkit is not bug-free

Page 91: Desktop apps with node webkit

But it is an impressive piece of work

Page 92: Desktop apps with node webkit

It’s so good, that Intel replaced their use of Java and Chrome in their IDE with Node Webkit

Page 93: Desktop apps with node webkit

Intel XDKxdk.intel.com

Page 94: Desktop apps with node webkit

And Roger now works for Intel in China

Page 95: Desktop apps with node webkit

Oh, and Roger is speaking at LXJS

Page 96: Desktop apps with node webkit

There is still so much I still haven’t covered about Node Webkit.

!

I recommend reading more about it at the Github repo and online

Page 97: Desktop apps with node webkit

I hope you get a chance to play with it and create something amazing!

Page 98: Desktop apps with node webkit

@paulbjensen

Page 99: Desktop apps with node webkit

Thank you