Cross platform web app development

A presentation at Spain.js 2013 in July 2013 in Madrid, Spain by José M. Pérez

Slide 1

Slide 1

Cross platform web app development Tomás Pérez - @tomasperezv José M. Pérez - @jmperezperez

Slide 2

Slide 2

Cross platform application development Contents Web development in Spotify - Why and how - Benefits and learnings Key web technologies that we use - IFrames and postMessage: security and performance - Communication between different tabs Summary Questions

Slide 3

Slide 3

Cross platform application development Spotify <3 web

Slide 4

Slide 4

Cross platform application development Beginnings of Spotify

Slide 5

Slide 5

Cross platform application development Giving web a try ● Using CEF to move features to web ● Write features as webapps ● Sandboxed environment

Slide 6

Slide 6

Cross platform application development Giving web a try (2)

Slide 7

Slide 7

Cross platform application development What is a Spotify app? ● HTML ● CSS ● JS ● manifest.json

Slide 8

Slide 8

Cross platform application development What is a Spotify app? - Developer tools

Slide 9

Slide 9

Cross platform application development Architecture of the Spotify Apps Platform - overview

Slide 10

Slide 10

Cross platform application development Architecture of the Spotify Apps Platform - desktop

Slide 11

Slide 11

Cross platform application development Architecture of the Platform - all clients

Slide 12

Slide 12

Cross platform application development Multiple platforms CapturFiles_25.png

Slide 13

Slide 13

Cross platform application development Releasing Spotify Apps ● Decoupled system ● Provides gradual roll-out, and availability depending on platform and country

Slide 14

Slide 14

Cross platform application development Benefits ● Feature ownership by teams ● Fast development pace due to fewer dependencies ● Less hassle to deploy. Makes it easy to tweak a feature after being released ● 3rd party app development

Slide 15

Slide 15

Cross platform application development Only benefits? ● Performance in views with a lot of data on mobile ● Time to render

Slide 16

Slide 16

Spotify Embedded Platform Tomás Pérez twitter.com/tomasperezv github.com/tomasperezv

Slide 17

Slide 17

Cross platform application development Spotify Embedded Platform

Slide 18

Slide 18

Cross platform application development Content Platform architecture - IFrames and postMessage: security and performance Remote control technology - Communication between different tabs - Performance of the communication channel Performance improvements

Slide 19

Slide 19

Cross platform application development Architecture of the Platform

Slide 20

Slide 20

Cross platform application development Sandboxed platform using IFrames Good - Separated execution context - Better security and privacy - We control the release process Bad - More difficult communication - IFrames are expensive

Slide 21

Slide 21

Cross platform application development How expensive are IFrames? A lot(): () http://www.stevesouders.com/blog/2009/06/03/using-iframes-sparingly/

Slide 22

Slide 22

Cross platform application development Custom widgets in partner pages

Slide 23

Slide 23

Cross platform application development Cross-origin communication using postMessage

Slide 24

Slide 24

Cross platform application development Sending: content of the message frame.contentWindow.postMessage(message, ...); msg [in] Type: any One of the following: ● JavaScript primitive, such as a string ● object ● Array ● ... (*)[object Object]

Slide 25

Slide 25

Cross platform application development Security frame.contentWindow .postMessage(..., 'https://embed.spotify.com'); window.addEventListener('message', function(messageEvt) { if (messageEvt.origin === 'https://valid-domain.com') { ... } });

Slide 26

Slide 26

Cross platform application development postMessage transfer rate

Slide 27

Slide 27

Cross platform application development Remote controlling Spotify Clients Client Desktop: based on long polling + https Webplayer: communication with another browser window or tab - server side - postMessage - Cookies - localStorage

Slide 28

Slide 28

Cross platform application development Is localStorage always available? window.localStorage localStorage.setItem('key', 'value') Storage DOMException constructor: StorageConstructor code: 22 length: 0 constructor: DOMExceptionConstructor message: "QUOTA_EXCEEDED_ERR: DOM Exception 22"

Slide 29

Slide 29

Cross platform application development Communication between different tabs

Slide 30

Slide 30

Cross platform application development localStorage locks The UI Thread is stopped for some operations Firefox and Chrome wait until the first read is requested, then load all the items http://calendar.perfplanet.com/2012/is-localstorage-performance-a-problem/

Slide 31

Slide 31

Cross platform application development localStorage notifications via the storage event window.addEventListener('storage', function() {}, false); The event change is only emitted when the values actually change: use a timestamp if needed. {key: 'command', value: 'play', timestamp: 1373021690574}

Slide 32

Slide 32

Cross platform application development Can we trigger the event manually? var evt = document.createEvent('StorageEvent'); evt.initStorageEvent('storage', ...); window.dispatchEvent(evt); Yes...although not to communicate between different windows.

Slide 33

Slide 33

Cross platform application development localStorage communication strategies Options: A) Create a new item every time, but remove it once it’s received. B) New item, don't remove once it’s received, instead do it regularly. C) Reuse always the same item (use timestamp)

Slide 34

Slide 34

Cross platform application development Notification delay Chrome ~2 milliseconds IE10 ~34 milliseconds

Slide 35

Slide 35

Cross platform application development Performance improvements Prefetch / Prerender ● ● Rendering pages <link rel="prerender" href="..."> Fetching static resources <link rel="prefetch" href="...">

Slide 36

Slide 36

Cross platform application development Potential improvements Batching postMessages Webworkers

Slide 37

Slide 37

Cross platform application development Conclusions - Web apps have been a good choice to extend our functionality in multiple platforms. - Web technologies make relatively easy to create complex integrations to improve the user experience. - It is very important to monitor and analyze carefully the performance.

Slide 38

Slide 38

Cross platform application development Questions? Tomás Pérez @tomasperezv José M. Pérez @jmperezperez Slides, code examples and reference github.com/tomasperezv/spainjs-2013

Slide 39

Slide 39

Cross platform application development Thanks!