With the proliferation of IoT devices in the market the the recent surge of DIY as well as cloud-based “plug and play” home security solutions such as those from Dropcam, Simplicam, D-Link (and the resurgence of DIY solutions using Motion or ZoneMinder), many solution providers are looking at powerful mobile clients to monitor as well as control their home appliances, be it cameras or home lights as well as be notified of motion detection activities while away. This article will discuss how one can (and should) leverage modern hybrid frameworks such as Ionic, Meteor or similar solutions to develop a common code base that works across multiple platforms and yet not compromise on speed or performance. Specifically, this article will focus on Ionic, a very powerful MIT licensed framework based on AngularJS that sits on top of Cordova and bring out the advantages of such a solution.
One of the first things that one should assess is what a typical IoT application requires and then assess whether a hybrid framework is a right approach or a fully native solution. The following key traits likely apply to most IoT mobile clients:
While there are many other requirements, the list above is pretty much the core/important needs. IoT apps are almost always CRUD sort of applications – they are almost always interacting with some API, getting/setting/retrieving and rendering values at its core. This is unlike, say, a gaming application where the core requirements are high-speed graphics rendering/scaling (incidentally, don’t knock hybrid apps for gamers too – check out Phaser IO, a great example of how far web engines have come – fully hardware accelerated 3D transformations built right into your browser)
Simply put this means the following:
AngularJS is often referred to as “the super-heroic JavaScript MVW framework”. The ‘W’ stands for ‘Whatever’. Many people develop Angular using the standard MVC model, but Angular offers other models that may suit your programming needs. This post by one of the Angular authors summarizes this aptly.
Cordova has many plugins for native platform integration. Over the years, developers have added key platform specific plugins to Cordova which are all available to ionic. But on top of that, ionic folks regular keep updating ngCordova which is the Angular-ized version of many of these plugins. You can use either version – using the ngCordova version makes it much simpler to use than the standard Cordova Javascript based invocation. Here is a simple example of how one may use touchID but only if the platform is iOS using ionic: – This example uses $cordovaTouchId.
if ( $ionicPlatform.is('ios'))
{
$cordovaTouchID.checkSupport()
.then(function () {
// success, TouchID supported
$cordovaTouchID.authenticate("")
.then(function() {
console.log("Touch Success");
// Do unlock code here
},
function () {
console.log("Touch Failed");
});
}, function (error) {
console.log("TouchID not supported");
});
}
else
{
console.log("Not iOS, not checking for touchID");
}
That’s it. With just a few lines, you’ve integrated touch-ID into your app. Obviously, touchID is a platform-specific API only available on iOS which also tells you that it is pretty straight forward to integrate platform specific code into your code and ionic gives you all the platform check APIs needed to invoke code specific to a platform ($ionicPlatform.is is how we are checking). If you are concerned that there will be too much fragmentation, don’t worry, most of the angular plugins for Cordova work on both Android and iOS (and often Windows too), as long as what you need is not a feature that is closely tied to a specific platform. For example, the ngCordova $cordovaContacts plugin abstracts address book entries for all platforms – you don’t need to worry about how to handle them differently.
IoT apps are almost always data-centric. They are always fetching/displaying/rendering and processing fast-changing data (the data could be from sensors, cameras, or anything else). A great property of Angular is it’s very powerful data binding approach. The ‘view’ part of Angular, which is defined by HTML pages called ‘templates’ can use “angular expressions” and “angular variables” which create a binding between the model and its representation and Angular takes care of updating the value as it changes. Consider for example, a situation where you are retrieving a sensor value via an API and you need to display it in a view. Obviously, it is a bad idea to ‘freeze’ the app while you wait for data. You’d much rather keep rendering views as fast as possible for the app to be responsive and update the data when it is ready. So here is an example of how easy that is to do in AngularJS (and therefore ionic that is written on top of Angular)
This is the template code: (Note the angular variable sensorVal – its in double curly braces which means Angular will evaluate its value every cycle – called a digest cycle)
Sensor Value: {{sensorVal}}
$scope.sensorVal="loading...";
$http.get("https://mysensorcloud.com/api/getHeartRate.json")
.then ( function (success) {
$scope.sensorVal = success.data;
},
function (error) {
$scope.sensorVal = "error retrieving data: " + error.reason;
});
This is an example of using promises to handle deferred responses. The HTTP query can take time, but Angular knows that “sensorVal” is bound to the template view, so anytime sensorVal changes, the view gets updated (assuming you are in that view)
This is probably a favorite complaining point for many. Many believe programs written using HTML5 canvas (WebView) is not fast enough. One activity that is regularly complained about is scrolling performance. The premise of the argument is that as the list increases, a webview is simply not geared to handle large list scrolling. Well, good news when it comes to ionic:
Caveat:Specific for Android users, those who have tried using ionic and have faced performance issues are likely using versions of Android prior to 4.4. The problem here is that older versions of Android packaged a low performance webView and not chrome as the default webview. To work around this aspect, the cross-walk project was launched and ionic quickly adopted this too. Simply put, you can combine crosswalk into an ionic project and have it render within a chrome view which offers excellent performance. Note that this does increase your APK size by around 15-20MB. If that is a problem for you (is it really?) then you can look at crosswalk-lite that weights in at 10MB.
This is not really an Ionic feature but an Angular feature. One key compliant from many is it is hard to write re-usable code with well defined libraries when doing hybrid apps. This is simply not true. Angular has the following key code-reuse and organizational structures that we highly recommend you use:
someModule.controller('MyController', ['$scope', 'ServiceLogin', function($scope, 'ServiceLogin') {
...
ServiceLogin.doLogin();
...
}
...
}]);
This should really be at the top of the list. The bane of any application is asking an engineer with no UX experience to develop a screen. You will inevitably have a bright green button with a blue underlined hyperlink somewhere down the line, or, with the more experienced, views that suddently truncate when switched from landscape to portait. This was a little tongue-in-cheek but you’ve faced this problem, haven’t you? We’ve seen, literally, hundreds of cordova/phonegap applications with really bad UI screens. And here is why: Designing HTML5 UI’s is HARD. Different browsers have different ways of handling CSS. Even if you did know CSS, selecting the right UI colors, width, screens etc is not easy. Here is where ionic really shines:
Let’s make sure we cover all the IoT requirements we wrote about earlier. HTML5 (and therefore Ionic) supports a <video> tag that plays h464. And obviously the <img> tag for images, even progressive images that change every few seconds (this is how old motion capture systems work – they establish a long term HTTP connection that keeps streaming JPEGs over multipart/mime over a single TCP connection) And as far as the camera goes, we have CordovaCamera. And while we are at it, discussing plugins, nothing stops you from writing your own plugin. You’d do that in 3 steps:
Multiple protocols are easily supported, even HTTPS. Note that if you are using a self-signed certificate for HTTPS you need to make sure the certificate is installed on the phone as well. For WebSockets, there is this plugin that makes it very simple here is an example of how an http request looks like in Angular/ionic:
var myurl="https://myserver.com/api/v3/getSubscribers.json";
$http.get(myurl)
.then(
function(succes)
{
},
function(error)
{
});
That’s it. Incidentally, this also introduces us to a very powerful (and confusing, if you don’t understand it) construct called ‘promises’ that make it very simple to handle asynchronous operations very easy. For example: let’s suppose you make an HTTP query and it takes 10 seconds for the server to respond. By that time you have navigated away to some other screen. What happens to the response? Promises stick around to handle the response easily in that code block.
Ionic fully supports push notifications. It actually supports two versions:
There is nothing here that is specific to angular or ionic, but there are many libraries out there to get you started For example $cordovaOauth for logging in via many oauth services like Facebook, Google+, etc.
There are literally several hundred of supported libraries for many things you want to achieve and most of them are MIT licensed (commercial software friendly). Before you go about writing your own, look at the following places:
There are really, incredible tools out there for graphing and visualization. Some of our favorites:
Surprised? Aren’t javascript apps a nightmare to debug? Generally yes, but here are some incredible things developers should leverage:
See the Pen Animated radial / circular menu by Creative Punch on CodePen.
Just like surfers are always reminded to respect the waves, no matter how good they are, we like saying ‘respect the DOM’. As simple as AngularJS and Ionic looks, writing a good app needs sound design principles when you go beyond writing the simplest of apps. These days ionic (and similar frameworks) are being used to write very intensive apps like chat clients to video calling clients to full featured IoT mobile apps and there are many things to watch out for. Some items worth remembering:
Hybrid apps have come a long way. The offer native like performance with the benefits of code-reuse across platforms. Before you consider going the native way, consider if your team knows enough about hybrid apps. Have them explore Ionic (the focus of this post), or alternate tools like Meteor If you are considering cordova (phonegap), we strongly recommend you don’t stop at just phonegap/cordova. Use a platform like Ionic on top of it, bring in the power of AngularJS and watch your apps get a new life.