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 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.
There are a few of things we are going to need do to get started.
- Signup for a PubNub account.
- Download pubnub.js
- 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.
- 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.
Ok, let’s go real-time.
- Copy & Paste the Gist below into your app.js file.
- Login to pubnub.com.
- Click on the “Dev Console” Link.
- Change the channel to “tidev_demo” and click the “Subscribe” button.
- 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.
- 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!
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.