Friday, September 27, 2013

Getting Started with Mobility, Part 10: A Back-end in the Cloud with Windows Azure Mobile Services

In this series of posts, we're looking at how to get started as a mobile developer. In Parts 1-9, we examined a variety of mobile platforms and client app development approaches (native, hybrid, web). We've seen a lot so far, but that's only the front-end; typically, a mobile app also needs a back-end. We'll now start looking at various approaches to providing a back-end for your mobile app. Here in Part 10 we'll look at Microsoft's cloud-based Mobile Backend As A Service (MBAAS) offering, Windows Azure Mobile Services.


About Mobile Back-end as a Service (MBaaS) Offerings
To create a back-end for your mobile app(s), you're typically going to care about the following:
  • A service layer, where you can put server-side logic
  • Persistent data storage
  • Authentication
  • Push notifications
You could create a back-end for the above using many different platforms and technologies, and you could do so in a traditional data center or in a public cloud. You'd need to write a set of web services, create a database or data store of some kind, provide a security mechanism, and so on.

What's interesting is that today you can make a "build or buy" decision about your mobile back-end: several vendors and open source groups have decided to offer all of the above as a ready-to-use, out-of-box service. Microsoft's Windows Azure Mobile Services is an example of this. Of course, it doesn't do all of your work for you--you're still going to be responsible for supplying a data model and server-side logic. Nevertheless, MBaaS gives you a huge head start. MBaaS is especially valuable if you want to focus your time on your mobile app, not the supporting elements.


Windows Azure Mobile Services
Windows Azure Mobile Services "provides a scalable cloud backend for building Windows Store, Windows Phone, Apple iOS, Android, and HTML/JavaScript applications. Store data in the cloud, authenticate users, and send push notifications to your application within minutes." Specifically, that means you get the following:
  • Authentication (to Facebook, Twitter, Microsoft, or Google accounts)
  • Scripting for server-side logic
  • Push notifications
  • Logging
  • Data storage
  • Diagnostics
  • Scalability
The service will also generate for you starter mobile clients for iOS, Android, Windows Phone, Windows 8, or HTML5. You can use these apps as your starting point, or as references for seeing how to hook up your own apps to connect to the service.


Pricing
So what does all this back-end goodness cost? At the time of this writing, there are Free, Standard ($25/month), and Premium ($199/month) tiers of pricing. You can read the pricing details here.


Training
We reference training resources throughout this post. A good place to start, though, is here:

Get Started with Mobile Services
Android    iOS    Windows Phone    Windows 8    HTML5


Provisioning a Mobile Service
The first thing you'll notice about WAMS is the care that's been given to the developer experience, especially your first-time experience. Once you have a Windows Azure account, you'll go to azure.com, sign-in to the management portal, and navigate to the Mobile Services tab. From there, you're only a handful of clicks away from rapid provisioning of a mobile back-end.

1 Kick-off Provisioning
Click New > Mobile Service > Create to begin provisioning a mobile service.

Provisioning a Mobile Service

2 Define a Unique Name and Select a Database
On the first provisioning screen, you'll choose an endpoint name for your service, and either create a database or attach to one you're previously created in the cloud. The service offers a free 20MB SQL database. You'll also indicate which data center to allocate the service in (there are 8 worldwide, 4 in the U.S.)

Provisioning a Mobile Service - Screen 1


Provisioning a Mobile Service - Screen 2

3 Wait for Provisioning to Complete
Click the Checkmark button, and provisioning will commence. It's fast! In less than a minute your service will have been created.

Newly-provisioned Mobile Service Listed in Portal

4 Use the New Mobile Service Wizard
Click on your service to set it up. You'll be greeted with a wizard that walks you through. This is especially helpful if this is your first time using Windows Azure Mobile Services. On the first screen, you'll indicate which mobile platform you are targeting: Windows Store (Windows 8), Windows Phone 8, iOS, Android, or HTML5 (don't worry, you're not restricted to a single mobile platform and can come back and change this setting as often you wish).

Setup Wizard, Screen 1

5 Generate a Mobile App that Uses your Service
Next, you can download an automatically generated app for the platform you've selected, pre-wired up to talk to the service you just provisioned. To do so, click the Create a New App link. This will walk you through 1) installing the SDK you need for your mobile project, 2) creating a database table, and 3) downloading and running your app. The app and database will initially be for a ToDo database, but you can amend the database and app to your liking once you're done with the wizard.

Generating a Mobile Client App for Android
 
In the next section, we'll review how to build and run the app that you generated, and how to view what's happening on the back end.
 

Building and Running a Generated Mobile App
Let's walk through building and running the To Do app the portal auto-generates for you. The mobile client download is a zip file, which you should save locally, Unblock, and extract to a local folder. Next, you can open the project and run it--it's that simple to get started.

Running the App
When you run the app, you'll see a simple ToDo app--one that is live, and uses your back-end in the cloud for data storage. Run the app and kick the tires by adding some tasks. Enter a task by entering it's name and clicking Add. Delete an item by touching its checkbox.

To Do app running on Android phone

Viewing the Data
Now, back in the Windows Azure portal we can inspect the data that has been stored in the database in the cloud. Click on the Data link at the top of the Windows Azure portal for your mobile service, and you'll see what's in the ToDo table. It should match what you just entered using the mobile app.

Database Data in the Cloud


Dynamic Data
One of the great features of Windows Azure Mobile Services is its ability to dynamically adjust its data model. This allows you to change your mobile app's data structure in code, and the back-end database will automatically add new columns if it needs to--all by itself.

Get Started with Data in Mobile Services
Android    iOS    Windows Phone    Windows 8    HTML5


Dynamic data is a great feature, but some people won't want it enabled, perhaps once you're all ready for production use. You can enable or disable the feature in the Configure page of the portal.

Server-side Logic
Windows Azure Mobile Services happens to use node.js, which means server-side logic is something you write in JavaScript.

Mobile Services Server Script Reference

You can have scripts associated with your database table(s), where operations like Insert, Update, Delete, or Read execute script code. You set up these up on the Data page of the portal for your mobile service.

Database Action Scripts
 
Here's an example of a Read script that restricts processing to only return results owned by the current authenticated user:
 
function read(query, user, request) {    
    query.where({        
        owner: user.userId
     });    
    request.execute();
}
 

You can also set up scheduled scripts, which run on a schedule. On the Schedule page of the portal, click Create a Scheduled Job to define a scheduled job.

Creating a Scheduled Job

Once you've define a scheduled job, you can access it in the portal to enter script code and enable or disable the job.

Setting a Job's Script Code

Authentication
You can authenticate against Microsoft accounts, Facebook, Twitter, or Google. This involves registering your app for authentication and configuring Mobile Services; restricting database table permissions to authenticated users; and adding authentication to the app.

Get Started with Authentication
Android    iOS    Windows Phone    Windows 8    HTML5


Push Notifications
Push notifications support is provided for each mobile platform. Tutorials acquaint you with the registration and code steps needed to implement push notifications for  each platform,

Get Started with Push Notifications
Android    iOS    Windows Phone    Windows 8    HTML5


Summary
Windows Azure Mobile Services provides a fast and easy mobile back-end in the cloud. It offers the essential capabilities you need in a back end and supports the common mobile platforms. If you're comfortable expressing your server-side logic in node.js JavaScript, this is a compelling MBaaS to consider.

No comments: