Top Banner
Mobile Web Architecture Structure, architecture and capabilities of hybrid mobile applications
107

Architecture app

May 19, 2015

Download

Technology

Ynon Perek

Hybrid mobile application architecture considerations
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Architecture app

Mobile Web ArchitectureStructure, architecture and capabilities of hybrid mobile applications

Page 2: Architecture app

Contact Me

• Ynon Perek

[email protected]

• http://ynonperek.com

Page 3: Architecture app

Agenda

Introduction to Hybrid Apps (+Phonegap) 1

HTML5 Features for Hybrid Apps 2

Push Notifications 3

Page 4: Architecture app

Why is mobile different?

Page 5: Architecture app

Questions To Ask for Mobile• How does my solution:

• Affects battery life ?

• Affects network traffic ?

• Deal with interruptions ?

• Deal with connectivity ?

• Respects user’s privacy ?

Page 6: Architecture app

Choosing Mobile Technology

Page 7: Architecture app

Problems With Native

• Too many different programming languages

Page 8: Architecture app

Problems With Native

• Too many different programming languages

• “Same app”, different code

Page 9: Architecture app

Web is not perfect either

• Code runs in a browser:

• Slower

• Restricted by the browser

Page 10: Architecture app

Web vs. Native

AppApp

Browser

Native Web

Page 11: Architecture app

Hybrid Apps

Web Content

Native App Wrapper

Page 12: Architecture app

Demo: Implementing A Hybrid Solution

Page 13: Architecture app

Hybrid Architecture

Web Content

Native Wrapper

Server + DB

3rd party extensions

Page 14: Architecture app

Takeaways

• Hybrid architecture provides the good AND bad from both worlds

• Understanding the platform is necessary

Page 15: Architecture app

Hybrid Apps +

• Most of the app is written once

• Only native parts are written again and again

• “Feels” native

• Developer controls native code => can use native APIs

Page 16: Architecture app

Hybrid Apps -

• Complex code

• Requires knowledge in many programming languages

• Data is transferred between environments

• Hard to debug

Page 17: Architecture app

Let’s Analyse A Hybrid App• Hystagram is a hybrid

version of instagram with minimal functionality

• Supports:

• Taking photos

• Watching photos

• Uploading photos to server

Page 18: Architecture app

Processes and scopes

Process Scope Why

Take a photo

Upload photo to server

View photo stream

Page 19: Architecture app

Processes and scopes

Process Scope Why

Take a photo Native Need to show a camera overlay

Upload photo to server

View photo stream

Page 20: Architecture app

Processes and scopes

Process Scope Why

Take a photo Native Need to show a camera overlay

Upload photo to server Native Upload must continue when app is not active

View photo stream

Page 21: Architecture app

Processes and scopes

Process Scope Why

Take a photo Native Need to show a camera overlay

Upload photo to server Native Upload must continue when app is not active

View photo stream Web Because we can

Page 22: Architecture app

Introducing Phonegap

Page 23: Architecture app

Introducing Phonegap

• An open source connection layer between web content and native

• Extensible

• Supported platforms: iOS, Android, Blackberry, WebOS, Windows Phone, Symbian, Bada

Page 24: Architecture app

Q & A

Page 25: Architecture app

Agenda

Introduction to Hybrid Apps (+Phonegap) 1

HTML5 Features for Hybrid Apps 2

Push Notifications 3

Page 26: Architecture app

The Web SideUsers and Sessions Location Camera Video Audio

Page 27: Architecture app

Web 101

HTTP Request

HTTP Response

Page 28: Architecture app

HTTP and state

• HTTP is stateless

• This means server has no way to know two requests originated from the same client

Page 29: Architecture app

Cookies

• Bits of data saved on a device and sent to the server with each HTTP request

• Used to store state

• Demo

Page 30: Architecture app

Session Management

Client Server

Login: user, pass

DB

Page 31: Architecture app

Session Management

Client Server DB

Login: user, passVerify: user, pass

Page 32: Architecture app

Session Management

Client Server DB

Login: user, passSave session

Session id

Page 33: Architecture app

Session Management

Client Server DB

Login: user, passSave session

Session idSet Cookie: Session id

Page 34: Architecture app

Session Management Options

• Session id on client, Session data on server

• Encrypted session data on client, key on server

• RESTful

Page 35: Architecture app

RESTful Web Services

• Server provides resources

• Resources are in known conventional URLs

Page 36: Architecture app

RESTful Web Services

/collection /collection/item

GET Returns an array of items

Returns details for a specific item

POST Create a new item

PUT Modify an existing item

DELETE Delete the collection Delete an item

Page 37: Architecture app

RESTful authentication

• Client uses a private authentication token to access restricted resources

• Demo: https://developers.facebook.com/tools/explorer

Page 38: Architecture app

What You Should Use

Page 39: Architecture app

What You Should Use

• For web clients, cookies are the easiest

• If possible, prefer to store only session id in the cookie

• For other clients, consider a token

Page 40: Architecture app

Q & A

Page 41: Architecture app

Geolocation

• Detect where your user is now

• Show nearby places

• Display location aware data

• Enable location specific features

Page 42: Architecture app

Technologies

• GPS

• A-GPS

• Cell Information

• WiFi Positioning

Page 43: Architecture app

GPS

• Global Positioning System

• Accuracy error: 2-10m

• Requires clear sky view

• Time to position: 5 seconds - 5 minutes

Page 44: Architecture app

A-GPS

• Uses both GPS chip and network information to get location

• Much faster to get location

Page 45: Architecture app

Cell Information

• Uses cell towers to calculate a device’s location

• Accuracy: A block or up to some km

• Time to location: immediate

Page 46: Architecture app

WiFi Positioning

• Detect location using a list of wireless routers in your area

• Relies on existing router databases

Page 47: Architecture app

Permissions

• Browser asks user permissions before sharing location

• Best practice: Request location just before using it

Page 48: Architecture app

Location Failures

• Location services don’t always work

• Why ?

• What do you do when you’re lost ?

Page 49: Architecture app

Location API

1 navigator.geolocation.getCurrentPosition(

2 successCallback,

3 failureCallback, 4 {

5 timeout: 0, 6 maximumAge: 60000,

7 enableHighAccuracy: false 8 });

Page 50: Architecture app

Using A Map

Page 51: Architecture app

Show Me The Map

• Both iOS and Android have built in maps applications

• Open the built-in maps application using a simple link

• Demo

Page 52: Architecture app

Built-In Maps +

• Great UI

• User feels “at home”

Page 53: Architecture app

Built-In Maps -

• User leaves the app

Page 54: Architecture app

When To Use

• Use built-in maps app for navigation

• Use embedded maps for geo-data related to your app

Page 55: Architecture app

Map APIs

• Google Maps

• Open Street maps

• Demo:https://developers.google.com/maps/documentation/javascript/examples/map-simple

Page 56: Architecture app

Q & A

Page 57: Architecture app

Taking Photos

Page 58: Architecture app

getUserMedia

• Desktop browsers support the new getUserMedia API

• Demo: http://shinydemos.com/photo-booth/

Page 59: Architecture app

getUserMedia Browser Support

Page 60: Architecture app

Using The Camera

• iOS >= 6; Android >= 3

• Normal <input> field opens camera app<input type="file" capture="camera" accept="image/*" id="takePictureField">

Page 61: Architecture app

What You Can Do

• Take a picture and send it to server

• Analyse picture on the client - Demo

• Apply filters or edit - Demo

Page 62: Architecture app

What You Still Can’t Do

• Can’t check if camera is available

• No camera overlay

Page 63: Architecture app

Q & A

Page 64: Architecture app

VIDEOTHE EASY WAY

Page 65: Architecture app

Video Tag

• HTML5 introduces a <video> tag to embed videos in a web page.

• Different browsers support different video formats. The <video> tag can specify multiple formats.

Page 66: Architecture app

Video Formats

• Video formats are like languages

• The same video can be encoded in different formats

• A browser must “speak the language” to be able to play the video

Page 67: Architecture app

Video Converter

• Miro is a free open source video player and converter

• http://www.getmiro.com

Page 68: Architecture app

Browser Support

• HTML5 spec does not define a video codec to use

• h.264 is the most widely supported. Plays on iPhone and Android

Page 69: Architecture app

The Markup

<video poster=”star.png”> <source src=”zebra.mp4” /> </video> !

Page 70: Architecture app

Limitations: Autoplay

Apple has made the decision to disable the automatic playing of video on iOS devices, through both script and attribute implementations.”

Issue 159336: On Android, html5 video autoplay attribute does not work.

Page 71: Architecture app

Limitations: Play Inline

Videos play inline for Android >= 3

Currently, Safari optimizes video presentation for the smaller screen on iPhone or iPod touch by playing video using the full screen

Page 72: Architecture app

Playing AudioHTML5 Audio Audio API

Page 73: Architecture app

Audio Tag

• New addition of HTML5

• Simple to use

• Both HTML tag and JS api

Page 74: Architecture app

Audio Tag

<audio src="scifi002.mp3" controls="YES"></audio>

Page 75: Architecture app

Audio Tag JS API

audio.src = “http://static1.grsites.com/archive/sounds/scifi/scifi002.mp3"; audio.pause();audio.currentTime = 0;audio.play();

Page 76: Architecture app

Audio Tag

• Demo

• Play a gun fire sound when button is clicked

Page 77: Architecture app

Audio Tag Limitations on Mobile

• Not accurate (has short delays)

• Starts to load sound after user interaction

• Can’t control the audio data

Page 78: Architecture app

Audio API

• A new improved API for playing sounds

• Timing considerations built-in

Page 79: Architecture app

Audio API Browser Support

Page 80: Architecture app

Audio API Architecture

Audio Context

Source Destination

Page 81: Architecture app

Audio Context

Source Destination

Gain (volume)

Page 82: Architecture app

Quiz

How do you implement cross fade ?

Page 83: Architecture app

Cross Fade

Audio Context

Source

Destination

Gain (volume)

Source Gain (volume)

Page 84: Architecture app

Audio API: What You Can Do

• Apply audio filters (using filter nodes)

• Change volume (using gain nodes)

• Merge multiple audio sources to one destination

• Play with accurate timing

Page 85: Architecture app

Audio API Demo

• Same game as before

• This time no delays in playing the audio

• More complex demo:http://labs.dinahmoe.com/ToneCraft/#

Page 86: Architecture app

Recording Audio

• getUserMedia() will allow audio recording

• Not yet supported on mobile

Page 87: Architecture app

Q & A

Page 88: Architecture app

Agenda

Introduction to Hybrid Apps (+Phonegap) 1

HTML5 Features for Hybrid Apps 2

Push Notifications 3

Page 89: Architecture app

User’s Perspective

Page 90: Architecture app

Marketer’s Perspective

For the first time in human history, you can tap almost two billion people on the

shoulder.

Page 91: Architecture app

Developer’s Perspective

• What is a notification ?

Page 92: Architecture app

Developer’s Perspective

• What is a notification ?

• User visible information reflecting some event

Page 93: Architecture app

Developer’s Perspective

• Why should I use notifications ?

Page 94: Architecture app

Developer’s Perspective

• Why should I use notifications ?

• Ensure time-sensitive delivery when your app isn’t running

Page 95: Architecture app

Developer’s Perspective

• How does push compared to poll ?

Page 96: Architecture app

Developer’s Perspective

• How does push compared to poll ?

• Pushes are server driven and immediate, polls are app driven and latent

Page 97: Architecture app

Keep In Mind

• Push notifications are not reliable

• (even if the APN server gets them)

Page 98: Architecture app

Types Of Notifications

• Badges (iOS only)

• Alerts

Page 99: Architecture app

Push Service Architecture

Your Server

Push Notification

Server

User’s Device

Page 100: Architecture app

Getting A Token

Page 101: Architecture app

Sending A Notification

Page 102: Architecture app

Demo App

• A simple push chat room hybrid app

• HTML layer handles user interface

• Native layer receives notifications

Page 103: Architecture app

Server: Sending A Notificationmy $notifier = $APNS->notify({ cert => "cert.pem", key => "key.pem", passwd => "1234" }); !$notifier->devicetoken($device_token); $notifier->message("message"); $notifier->sound('default'); $notifier->custom({ custom_key => 'custom_value' }); $notifier->sandbox(1); !$notifier->write;

Page 104: Architecture app

APNS Best Practices

• Keep the connection open

• Use multiple connections to the gateway

• Be polite with your users

Page 105: Architecture app

Q & A

Page 106: Architecture app

Thanks For Listening

• Ynon Perek

[email protected]

• http://ynonperek.com

Page 107: Architecture app

Photos Used In The Slides

• Choices (slide 6):http://www.flickr.com/photos/danmoyle/11715566974/sizes/l/

• Man with camera (slide 43):http://bit.ly/1gb5ZZM