Build

Advanced Presence Features using PubNub AngularJS Library

5 min read Michael Carroll on May 14, 2014

Advanced Presence using PubNub AngularJS LibraryA few weeks ago, we posted our using PubNub with AngularJS SDK for real-time which is a fast-growing and highly productive library for web application development in JavaScript. Since that time, a few awesome things have happened:

  • AngularJS continues to get more and more popular, and its pace of innovation shows no signs of slowing down(!)
  • A ton of developers have integrated the PubNub AngularJS library into their apps and have given us awesome feedback directly to improvements
  • Lastly and most importantly for the purposes of this blog, PubNub has released a huge new set of improvements to its Presence API, and those updates are now integrated into the AngularJS library

The new presence updates make it possible to add extended metadata to user and device connections, for example, “firstName”, “lastName” and “location” for a user, or “machine id” or “device id” in the case of a computer system or mobile device. Note: these choices are just arbitrary, you can choose whatever fields you want for your application!

The PubNub AngularJS SDK should look familiar to experienced developers in Angular app development as well as to folks who have explored the PubNub API a bit. For this blog entry, we’ll presume you are somewhat familiar with the ideas from an earlier PubNub AngularJS SDK blog, at least enough to get started. If you like what you see here, you can jump right into a live version of the app at https://pubnub.github.io/angular-js/#/chat or the source code at https://github.com/pubnub/angular-js/ .

Let’s take a look at the contents of https://pubnub.github.io/angular-js/index.html , the main entry point for the application.

Quick Recap: Setting Up AngularJS

You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization, for example:

Here are the script includes you’ll need to get started:

The web app is Angular-enabled with an ng-app attribute:

The code for our app lives in:

The app requires a dependency on the PubNub Angular library (pubnub.angular.service, defined in pubnub-angular.js):

The code for our controllers lives in:

Once that’s set, the PubNub service can be injected into the controller by name!

Quick Recap: AngularJS Publish & Subscribe

Publishing to channels is trivial – just use the PubNub.ngPublish() method.

Subscribing to channels is accomplished by calling the PubNub.ngSubscribe() method. After the channel is subscribed, the app can register root scope message events by calling $rootScope.$on with the “event name” string returned by PubNub.ngMsgEv(channel).

Here’s where it gets interesting – with the NEW presence extensions, it’s easy to associate attributes to your connection – as simple as:

This makes it much easier to create presence lists, rosters, and location-aware applications. For example, a location-aware application might use the NEW ngState() call to continuously update location metadata as follows:

The location fields “lat” and “lon” can then be used to display markers on the application’s map display. How cool is that?

Wiring Up Advanced Presence Events into your AngularJS Application

The Presence API makes it very easy to build presence-aware applications. Without the Presence API, you’d have to track all of the join/leave/timeout events yourself on the server side, which is really tough if you’re building a pure JS web application with no server side! However, Presence fixes that.

To keep things simple, wiring up Presence events using the PubNub AngularJS API is very similar to wiring up Message events. It all boils down to registering an event handler callback for the presence events:

In this case, PubNub.ngPrsEv(theChannel) returns the “event name” string that identifies presence events for the specified channel.

The presence event payload contains a bunch of useful information for your app: the channel and PubNub event itself. In the case of a single-user event, which you can tell by the presence of the ‘uuid’ field, payload.event contains action (join/leave/timeout), occupancy (number of users in the channel), timestamp, and uuid of the relevant user. Now, with the NEW advanced presence API, it also includes a data attribute (accessible through payload.event.data).

In the case of a multi-user event (such as the one triggered by ngHereNow()), the payload.event contains the occupancy count as well as a ‘uuids’ field (note pluralization) which contains the list of current user ids. Now, with the NEW advanced presence API, each uuid is an object that includes a uuid field and a state field which includes the extended attributes. Pretty nifty!

In addition to the callback-based API, there is also a convenient collection-based API that keeps track of channel membership automatically. To obtain the list of users, just call the ngListPresence() function with the name of the channel you’d like to list. Of course, you’ll want to already be subscribed to the channel and initialized the ‘here now’ status as in the example below.

Often times, we combine both approaches to make sure that our Angular views always have the most up-to-date user list:

But here’s the kicker – to make things extra awesome for Angular development, we also keep track of the presence extended attributes for the connected users in each channel. You can get it all simply by calling ngPresenceData and passing in the channel name. This is pretty sweet:

This gets the cached map of user UUIDs to extended attributes and stores it to the Angular scope. Now, every time the presence information changes, your app can access it via the $scope.userDataMap collection.

And just like that, your app is wired for presence!

One More Thing! New “WhereNow” Support for User Channel Subscriptions

All of this is really cool, but sometimes it’s hard to keep track of what users are subscribed to what channels in the application. Even with the cool presence helper functions in the PubNub Angular SDK, it’s not possible to know what users are present in what channels if you’re not subscribed to them.

With the new presence API, that all changes! There’s a nifty new ngWhereNow() function you can call to get access to global channel subscription information for a given user UUID.

The response contains a channels list of channel names for the subscriber, which can come in handy when you need to see exactly where a user’s at!

Wrapping Up

In this blog entry, we had fun showing you how to integrate your angular app with the advanced presence features of the PubNub AngularJS library. We hope you find this information to be useful — it is really cool to see the number of PubNub and AngularJS applications growing over the past few weeks!

The PubNub API has many more features we didn’t cover in this blog post, but which are explained in detail in the GitHub API Guide and Reference. The documentation walks you through additional topics which really enhance your real-time-enabled web application.

In future blog posts, we’ll cover even more outstanding features of the PubNub Angular API. In the meantime, please give the AngularJS integration a try, have fun, and reach out if you have ideas visit GitHub pubnub/angular-js. Or, do you need a hand (help@pubnub.com)!

Get Started
Sign up for free and use PubNub to power AngularJS
user detection

0