Building Mobile Apps with Apache UserGrid, the Open Source Baas

50
Dave Johnson Apigee Building Mobile Apps with Apache Usergrid, the Open Source BaaS

Transcript of Building Mobile Apps with Apache UserGrid, the Open Source Baas

Page 1: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Dave Johnson Apigee

Building Mobile Apps with Apache Usergrid, the Open Source BaaS

Page 2: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 2

Agenda

» Overview: why Usergrid » Exploring the Usergrid API » Creating an app with Usergrid

» v1 - Add Logins & Signup » v2 - Add Checkins » v3 - Add Follower relationships » v4 - Package & run via Apache Cordova

» Running & Deploying Usergrid » Q & A

Page 3: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Usergrid Overview

Page 4: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 4

Why Usergrid?

» Don’t build a server

PHPRuby Java

NodeMySQL PaaS

Services in the “cloud”

App Server Cloud

Page 5: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 5

Why Usergrid?

» Focus on building your app

Services in the “cloud”

App Cloud

Page 6: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 6

What is Usergrid?!

» Mobile Backend as a Service (mBaaS) !

» Backend as a Service (BaaS) !

» Database as a Service (DBaaS) !

» REST API wrapped around Cassandra DB

Page 7: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 7

What is Usergrid?

» A Web Application !

» A Management Portal !

» Client SDKs » iOS, Android, JavaScript, Node, Ruby,

Java, Perl, .Net, Command-line, …

Page 8: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 8

What is Usergrid?

» Core services you need to build apps: » JSON object indexing & query » Geo-location & geo-queries » Connections & Collections » Users, Groups, Social Graph » Authentication, Roles & Permissions » Asset & File Management !

» Soon: Push Notifications

Page 9: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 9

What is Usergrid?

Page 10: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 10

An Apache Incubator Project

Page 11: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Exploring the Usergrid API

Page 12: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 12

Usergrid REST API

» JSON over HTTP » POST, GET, PUT and DELETE JSON Entities !

» Entities have UUID, Name, Type » Entities have name/value properties !

» All Entity properties are indexed » SQL-like syntax for Entity queries

Page 13: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 13

Usergrid REST API

»Each application has root URL like this »http:// host/${org}/${app} !

» Entities exist in Collections »http:// host/${org}/${app}/${collection} !

» And you can define Collections of custom Entities with any properties you want

Page 14: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 14

Usergrid REST API

» There are special built-in Entity Collections: » http:// host/${org}/${app}/users » http:// host/${org}/${app}/groups » http:// host/${org}/${app}/roles » http:// host/${org}/${app}/activities » http:// host/${org}/${app}/assets

Page 15: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 15

Connections

» Entities can have Connections to other Entities !

» For example, to get all Entities that Dave is connected to via a connection of type “pets”

!» http:// host/${org}/${app}/users/dave/pets

Page 16: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 16

Collecting Cats

» Demo the Portal !

» Create Application » Create User Dave with Default Role » Create Cats Collection » Setup “Dave has Cats” Connections

Page 17: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Creating an app with Usergrid

Page 18: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 18

Let’s create a checkin app

» A simple checkin app to demonstrate Usergrid features: !

» Login & signup » Roles & Permissions » Following, Followers and Checkins » Usergrid JavaScript SDK !

» Built with jQueryMobile & Cordova

Page 19: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 19

Define the data model

» Usergrid provides the basic REST API !

» You must decide: » Entity Types & Properties » Entity Connections » Users Roles & Permissions

Page 20: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 20

Entities & Connections

» Entity Collections » Users » Checkins !

» Entity Connections » Users Follow Users » Users Have Checkins

Page 21: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 21

User Role: Guest

» User who has not signed up yet » Permissions User with Guest Role: !!!!

» Guest user POSTs to register » Usergrid takes care of account activation

GET POST PUT DELETE/users/* 🚫 ✅ 🚫 🚫

Page 22: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 22

User Role: Default

» Permissions for User with Default Role:

GET POST PUT DELETE/users/${user}/** ✅ ✅ ✅ ✅

/activities/* ✅ 🚫 🚫 🚫

/users/* ✅ 🚫 🚫 🚫

/users/${user} ✅ 🚫 ✅ 🚫

Page 23: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 23

App setup in the Portal

» Quick demo !

» Take a look at Roles & Permissions » Ensure they are setup right

Page 24: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Adding Login & Signup

Page 25: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 25

Getting started

» Pre-requisites: !» Usergrid JavaScript SDK !» jQuery Mobile !» Apache Cordova !» Your favorite dev environment

Page 26: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 26

Get setup in an IDE

Page 27: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 27

JavaScript SDK - login

var client = new Usergrid.Client({ appName: 'checkin1', orgName: 'test-organization', URI: 'http://10.1.1.161:8080' }); !client.login(username, password, function(err, res, entity) { if (err) { // inform user of error } else { // change to View Checkins page } });

Page 28: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 28

jQueryMobile

Page 29: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 29

JQuery Mobile page structure

<div data-role="page" id=“my-page-id“ data-theme=“a”> ! <div data-role="header" data-theme="a"> // header goes here </div> <div data-role="content"> // page goes here </div> <div data-role="footer" data-theme="a"> // footer goes here </div> </div>

Page 30: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 30

Apache Cordova

Page 31: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 31

Apache Cordova

» Hybrid approach to mobile apps: » Write code in with HTML5 & JavaScript » Package as a truly native app » Access device features: camera, contacts, etc. !

» iOS and Android, but also Amazon Fire, Blackberry, Windows Phone and more !

» Tool support: Adobe, Eclipse, Netbeans, IntelliJ, etc.

Page 32: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 32

Get setup in an IDE

» Demo time !!!!!!

» Create Cordova project in IDE » Add Login & Registration Page

Page 33: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Adding Checkins

Page 34: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 34

Checkins as Activities

» Usergrid supports notion of Activity » And supports a “Following” Connection !

» Users post Activities to their own Collection » POST /users/${user}/activities!!

» Users can see Activities those they follow » GET /activities

Page 35: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 35

JavaScript SDK - create checkin

var data = { type: “checkin", content: content, verb: “post", actor: { username: user.get(“username") } }; client.createUserActivity(user.get("username"), data, function( err, response, activity ) { if (err) { // alert user of error } else { // send user back to View Checkins page } });

Page 36: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 36

What we’ll add

» View Checkins Page !

» Add a Create Checkin Page !

» View Single Checkin Page

Page 37: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Add Follower relationships

Page 38: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 38

Usergrid Social Graph

» Usergrid provides Social Graph features including Following and Groups !

» Fred can follow Barney: » POST /users/fred/following/barney!

!» Fred can see his followers:

» GET /users/fred/followers

Page 39: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 39

JavaScript SDK - follow user

var target = // user to be followed var options = { method: 'POST', endpoint: ‘/users/' + me + '/following/users/' + target }; !client.request(options, function (err, data) { if (err) { alert("Unable to follow user " + target); } else { alert("Followed user " + target); } });

Page 40: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 40

What we’ll add

» View Users Page !

» View User Page with Follow Button

Page 41: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Package & run your app

Page 42: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 42

Apache Cordova

» Demo time !

» Use Cordova to launch the app in iOS

Page 43: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Running & Deploying Usergrid

Page 44: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 44

Developing with Usergrid

» Three ways to run Usergrid locally: » All-in-one Usergrid Launcher » Install Tomcat, Cassandra and Usergrid » Start Usergrid instance via Vagrant !

» Or find a BaaS provider that runs Usergrid: » e.g. Apigee’s API BaaS is free to use

Page 45: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 45

Usergrid deployment options

» Do it yourself with Tomcat, Cassandra and Puppet, Chef, etc. !

» Do it yourself on AWS via Usergrid AWSCluster » AWS Cloud Formation Template » Plus supporting Bash and Groovy scripts !

» Let somebody else run it for you… » Hint hint 😃

Page 46: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 46

Deployment Architecture UG1

Mobile

Cassandra Cluster

Load Balancer

Web Tomcats (shared nothing)

Page 47: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 47

Deployment Architecture UG2

Mobile

Cassandra Cluster

Load Balancer

ElasticSearch Cluster

Web Tomcats (shared nothing)

Distributed Queue

Page 48: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Wrapping up…

Page 49: Building Mobile Apps with Apache UserGrid, the Open Source Baas

© 2013 Apigee Confidential – All Rights ReservedThis work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.© Copyright Apigee 49

Questions?

» Apache Usergrid » http://usergrid.incubator.apache.org !

» Apigee API BaaS » http://goo.gl/zcjRxC !

» Dave Johnson » https://github.com/snoopdave/

» usergrid-mobile » usergrid-vagrant

» http://twitter.com/snoopdave » http://rollerweblogger.org/roller

Page 50: Building Mobile Apps with Apache UserGrid, the Open Source Baas

Thank you