PubNub – Taking Titanium Real-Time

The Project

I was recently working on a project where I wanted my users to make a change in the CMS and they could see the change in their app reflected almost immediately.

The Solution

The real-time web has always fascinated me so I have tried to keep an eye on the different players. I know there are loads of them out there but I am going to highlight one because it’s super fast, it’s free to play around with and it’s relatively inexpensive as you grow your app to be number 1 in iTunes. Most of all, they have a great commonjs module for easy integration with Titanium.

The Prep

There are a few of things we are going to need do to get started.

  1. Signup for a PubNub account.
  2. Download pubnub.js
  3. Create a new Titanium classic app (Not Alloy). For those of you that are asking “why not Alloy?” this can easily be adapted to work in Alloy, but for this demo I am going to keep everything in app.js for simplicity.
  4. Create a folder called “lib” in the same folder as your app.js file. It would be in your “Resources” directory of the new app we just created in the previous step.

The Steps

Ok, let’s go real-time.

  1. Copy & Paste the Gist below into your app.js file.
  2. Login to pubnub.com.
  3. Click on the “Dev Console” Link. Screenshot_1_29_14_10_13_AM
  4. Change the channel to “tidev_demo” and click the “Subscribe” button.
  5. On this page you will see 2 strings of letters and numbers. Go into your app.js file and replace –PUBKEY– and –SUBKEY– with the appropriate strings from the console page. Each of them will start with pub- or sub- so you know which is which.
  6. Compile the app and enjoy your real-time Titanium app.

What’s so great about PubNub or services like it? The possibilities are endless. You can create all sorts of amazing real-time solutions. Use it to communicate between servers, or between apps, or server to app. Be creative!

The Gist

This Gist was adapted from the example app posted here chat-example-app.

Please let me know if you have an issue with this Gist. How have you used PubNub or a similar real-time service? Drop us a comment below and share your creativity.

Josh Jensen is the technical director and a partner at ROAR. ROAR.pro is a leading mobile app company that helps nonprofits realize their potential in the mobile space. Josh has personally developed and launched hundreds of apps and overseen the development and deployment of all of ROAR's systems.


Comments

  • Joseph

    Thanks Josh, Will certainly look into it.

    Out of curiosity, wouldn’t the same Real-Time be done via ACS (Appcelerator Cloud) Push Notifications?

    I noticed PubNub supports desktop push. That, I haven’t seen much services/libs out there for that :)

  • Lysander

    Nice. I got this working. One quick note…In your gist code, the channel name should be ‘tidev-demo’ rather than ‘chat-room’ in order to match your directions.

  • Josh Jensen Post author

    Glad it’s working. Thanks for the note about the gist. I have updated it.