building a chat app with windows azure mobile

38
Building a cross-platform chat app with Microsoft Azure Mobile Services 14 may 2014, Timisoara Timisoara .Net Meetup #1

Upload: flavius-demian

Post on 22-May-2015

451 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Building a chat app with windows azure mobile

Building a cross-platform chat app with Microsoft Azure Mobile Services

14 may 2014, TimisoaraTimisoara .Net Meetup #1

Page 2: Building a chat app with windows azure mobile

Flavius Radu DemianSoftware developer, Avaelgo

I really like programming, especially web and mobile

Please feel free to ask questions any time and don’t be shy

Knowledge is power

[email protected] | [email protected] | @slowarad

Page 3: Building a chat app with windows azure mobile

Expectations

Learn how to use mobile services

Learn when to use mobile services

Learn the strengths and limitations of mobile services

Learn that mobile services wants to be friend with everyone

Make you curious => go home and play with it

Page 4: Building a chat app with windows azure mobile

Agenda

Overview

Storage

Custom API

Authentication & Authorization

Scheduler

Page 5: Building a chat app with windows azure mobile

Agenda

Diagnostics & Logging

Scale

Cool Third Party Add-Ons

Design of the chat app

Review

Page 6: Building a chat app with windows azure mobile

A visual representation

Page 7: Building a chat app with windows azure mobile

Overview

Mobile Services allows you to accelerate your mobile app development by providing a turnkey way to structure storage, authenticate users, and send push notifications.

With SDKs for Windows, Android, iOS, and HTML as well as a powerful and flexible REST API, Mobile Services lets you to build connected applications for any platform and deliver a consistent experience across devices.

Page 8: Building a chat app with windows azure mobile

Overview

Clients for: Windows Store, Windows Phone 8, iOS, Android, Javascript

You can add a cloud backend to your app in minutes without the need for server code

The backend login can be written in node.js or c# (preview)

The SDK’s are open source (on github) , it’s integrated with GIT

Page 9: Building a chat app with windows azure mobile

Overview

Easily build a backend for mobile apps and not only

Easily manipulate data (filters)

Easily authenticate users

Send push notifications

Integrate your favorite services and great community

Page 10: Building a chat app with windows azure mobile

Server side logic

Node.js

Is a software platform for scalable server-side and networking applications Applications are written in JavaScript can be run within the Node.js runtime on Windows, Mac OS X and Linux with no changes

It is written in

Page 11: Building a chat app with windows azure mobile

Storage

Windows Azure SQL Database

Dynamic schema on/off

REST API generated per table -> Data centric platform

Access your data through :Portal, SQL Management Studio, Rest API

Page 12: Building a chat app with windows azure mobile

Storage

JSON to SQL data types conversions

Page 13: Building a chat app with windows azure mobile

“Data Centric” Server Logic

Backend runs Node JS on small azure VM’s

“Interceptors” exposed for all CRUD requests to all tables

You get access to a predefined set of node modules :

request, console, push.*, tables, statusCodes, azure, mssql

Page 14: Building a chat app with windows azure mobile

Custom API

You can write your own API very easily and quickly

If you need you can add extra Node JS packages

exports.get = function(req, res) { /* code here */ }

exports.post = function(req, res) {/* code here */ }

Page 15: Building a chat app with windows azure mobile

Small demo time part 1

Login in the portal and create a mobile service

Set GIT credentials and get repository

Create some tables, show interceptors

Create a custom API

See list of predefined packages -> click here

Page 16: Building a chat app with windows azure mobile

Push notifications

The notification provider server maintains a "persistent IP connection" with your device in order to deliver notifications when the app needs to 'say' something to you.

Payload limited, specific to platform

The global push object is used to send push notifications

Success and Error callbacks are provided

Page 17: Building a chat app with windows azure mobile

Push notifications

Platform specific providers:

Page 18: Building a chat app with windows azure mobile

Push notifications overview

1) The app requests a channel from the Notifications Provider

2) The app sends the channel url to Mobile Services which stores it

3) When a notification is sent, Mobile Services executes something like this:

push.mpns.send(channelUri….)

Windows Phone case study

4) The notification goes through the Notifications Provider which forwards it to the device

Page 20: Building a chat app with windows azure mobile

Authorization

Table level authorization for CRUD operation

Everyone -> any request by anyone is accepted

Anyone with Application Key -> app key is sent on the request distributed (default)

Authenticated Users -> users authenticated with one of the mentioned identity providers

Page 21: Building a chat app with windows azure mobile

Authorization

Table level authorization for CRUD operation (*cont)

Scripts and Admins -> registers scripts or requests via the master key

The application key is not secure and should not be used to authenticate users of your app, especially in production

Page 22: Building a chat app with windows azure mobile

Scheduler

It runs jobs on simple or complex recurring schedules such as:

1) Send broadcast push notifications

2) Processing or resizing stored images

3) Invoking a Web Service over HTTP/s

4) Post a message to a Windows Azure Storage Queue, etc

Page 23: Building a chat app with windows azure mobile

Diagnostics and Logging

View diagnostics directly in the portal including :1) API calls2) CPU time 3) Data Out

LoggingConsole.* operations like console.log and console.error provide an easy means to debug your server side scripts.

Page 24: Building a chat app with windows azure mobile

Scale

Compute - scale between shared and reserved mode

Increase/decrease your instance count

Storage ability to scale out your mobile service tenant(s) to a dedicated SQL DB

Ability to scale up your SQL DB from web through business to 150GB. Since Microsoft Build it is up to 500GB .

Page 25: Building a chat app with windows azure mobile

Small demo time part 2

Push notifications

Authentication

Authorization

Diagnostics

Logging

Scale

Page 26: Building a chat app with windows azure mobile

Code cheat sheet - client

public MobileServiceClient MobileService = new MobileServiceClient(“dns”,“key"); public IMobileServiceTable<UserEntity> Users = App.MobileService.GetTable<UserEntity>();

public MobileServiceUser MobileServicesUser = await App.MobileService.LoginAsync(provider);

await UsersTable.InsertAsync(App.CurrentUser);

result = await App.MobileService.InvokeApiAsync<T>(“link”, HttpMethod.Get, extraParams);

App.MobileService.Logout();

Page 27: Building a chat app with windows azure mobile

Code cheat sheet – server (interceptor)

function insert(item, user, request) { console.log("item is: " + JSON.stringify(item));

var sql = "SELECT name FROM UserEntity WHERE providerIdLong = ? AND identityProvider = ?"; mssql.query(sql, [item.providerIdLong, item.identityProvider], { success: function(results) { if( results.length == 0){ results = null; } completeUserProfile(item, user, request, results); }, error: function(err) {

request.respond(statusCodes.BAD_REQUEST, { message: err }); } });}

Page 28: Building a chat app with windows azure mobile

Code cheat sheet – server (Api)

exports.get = function(req, res) { var userTable = req.service.tables.getTable('userEntity'); var userToExclude = req.query.userId; userTable.read({ success: function (items) { if (items.length > 0) { var finalResults = [], currentEntry = null; items.forEach(function(item) { if( item.providerIdLong != userToExclude){ currentEntry = new Object(); currentEntry.name = item.name;

finalResults.push(currentEntry); } }); res.send(statusCodes.OK, finalResults); } } }); };

Page 29: Building a chat app with windows azure mobile

Code cheat sheet – server (push)

if (entry.deviceType === "Android") //Android{

var payload = new Object();payload.content = item.content;payload.fromId = item.fromId;payload.fromPicture = item.fromPicture;payload.fromName = item.fromName;push.gcm.send(entry.registrationId, JSON.stringify(payload), {

success: function(response) { console.log('Android Push notification sent: ', response);}, error: function(error) { console.log('Android Error sending push notification android: ', error);}

});}

Page 31: Building a chat app with windows azure mobile

Let’s talk about the chat app

Page 32: Building a chat app with windows azure mobile

Design - Functionalities

Integrate Microsoft Azure Mobile ServicesIntegrate with social media providersStore data to cloud after auth ( user and channel related data )Retrieve the rest of the users you can talk to (custom API)Send message functionalityReceive message functionalityLogout functionality

Page 33: Building a chat app with windows azure mobile

Design – send push to web

connect

connect

connectmessage

message

message

to web

message

forward message to web

connect

Page 34: Building a chat app with windows azure mobile

Design - Database

Tables: users, channels, messages

ChannelChannelUri

RegistrationId

UserProviderId

DeviceType

MessageFromId

ToId

DeviceType

FromName

FromPicture

UserProviderId

Name

Picture

AccessToken

AccessTokenSecret

Page 35: Building a chat app with windows azure mobile

Let’s show some code

But first let’s see a short teaser

Page 36: Building a chat app with windows azure mobile

Review of Azure Mobile Services

Create a scalable and secure backend for your Windows, Android and iOS apps

Store data in the cloud

Easily authenticate users

Send push notifications

Page 37: Building a chat app with windows azure mobile

Review of Azure Mobile Services

Consume your favorite services

Monitor, alert, and auto scale

Cheap and FREE in some cases -> click here

Preview: No availability Service Level Agreement

Paid: General Availability: 99.9%

Page 38: Building a chat app with windows azure mobile

Thanks

Any questions?