Presentation from ConFoo 2014 on Windows Azure Mobile Services. Introducing the platform, building out an application that uses data storage, server-side scripts, custom API endpoints, push notifications, and client authentication. Source code is available on GitHub at http://github.com/goldshtn/rentahome
2. Who Are We? App Developers
What Do We Hate? Backends
3. We need one backend for all our
mobile apps, scaled by the cloud
Oh, and it should cost about $0-5 /
month for a small, simple app
4. Backend as a Service
Windows 8 & Windows Phone
iOS & Android
HTML & JavaScript
Data & LINQ queries
Server scripts, scheduler
Authentication & WAADNEW
Custom APINEW
Source control and NPMNEW
Push, notification hubsNEW
6. Initializing The Mobile Service
Client
The application key is for development
iOS
Windows 8 / Windows PhoneIn Android use user
purposes only. / production,
authentication to limit access to data
ms = new MobileServiceClient(
[MSClient clientWithApplicationURLString:
"https://rentahome.azure-mobile.net"
"https://rentahome.azure-mobile.net",
"..." /*API key*/);
applicationKey:"..."];
7. Accessing Data
// Windows 8 and Windows Phone
var apartments = await ms.GetTable<Apartment>()
.Where(a => a.Bedrooms > 2).ToListAsync();
// Android
ms.getTable(Apartment.class).where().
.field("bedrooms").gt(2).execute(...);
// iOS
NSPredicate *pred = [NSPredicate
predicateWithFormat:@"bedrooms > 2"];
[[client getTable:@"apartment"] readWhere:pred
completion:^...];
8. Server Scripts
CRUD operations can pass through a custom
script
Use for validation, data enrichment, etc.
Scripts are written in JavaScript and run on Node.js
Can access several Node modules:
request, push, …
function insert(item, user, request) {
if (item.address.length === 0) {
request.respond(400);
} else {
request.execute();
}
}
9. Custom API
Add custom HTTP endpoints to your mobile
service
Very useful for external access
exports.post = function(request, response) {
sharedHub.send_ad(request.body.message, function(error) {
if (error) {
response.send(500, 'Error sending ad: ' + error);
} else {
response.send(200);
}
});
};
11. Push
Prepare app for push from script
On trigger, send push notifications
Depends on platform
Unfortunately, push API differs for each platform
Channel = await PushNotificationChannelManager.
push.wns.sendToast02(channel.uri, {
text01: "New apartment added",
CreatePushNotificationChannelForApplicationAsync();
channelsTable.Insert(new Channel(Channel.Uri));
text02: apartment.address });
12. Notification Hubs
Register push messages based on the
Blast out templates for push notifications with
arbitrary and the tags
template custom tags
template =
var payload{=data: { message: '$(message)' } };
{ message : message };
hub.gcm.createTemplateRegistration(request.body.uri, });
hub.send(tag, payload, function(error, outcome) { ...tags,
template, registrationComplete);
16. Summary
Windows Azure Mobile Services provide a
powerful and customizable backend for your
mobile apps
Code available at
http://github.com/goldshtn/rentahome
Open the portal, show the general service settings and the dashboard that kind of gives you an app skeleton for free.Then, show the data tab with the tables we have, and basic data manipulation on these tables.Mention the concept of server scripts on CRUD operations – but don’t show these scripts yet.
Show the insert script on the apartments table.Explain that we’re using geocoding to obtain coordinates for the apartment, and we’re also pulling some information from Twitter (the user’s display name) – this will be explained later when we discuss authentication.
Show the channels table that contains the channels and the device types – this is necessary because the push semantics are different for each platform.Show the insert script on the apartments table again, and explain that it uses four different methods of the push module to deliver the push to the appropriate platform.
Demo the full app experience on all four platforms: login, add a new apartment, show push on other devices, delete an apartment, view maps (based on coordinates provided by the server).