Building a PubNub-Powered Chat App with Sencha Touch 2
Good News! We've launched an all new Chat Resource Center.
We recommend checking out our new Chat Resource Center, which includes overviews, tutorials, and design patterns for building and deploying mobile and web chat.
Sencha Touch 2 is a new framework for building super slick mobile applications. I decided to put on my mobile dev hat on and experience it for myself. And of course, I wanted to see how nicely it played with PubNub.
Turns out, it delivered. Sencha is not only intuitive, it's a blast. There were certainly a few “gotcha”s, but I found the experience to be extremely pleasant. Here's what I learned. First step is installation. This was actually not the best thing in the world. It wasn't abundantly clear what files / folders needed to exist in what places.
Picking up a new web framework
Furthermore, the documentation and “getting started” documents on the website were a completely distinct set from the ones that come when you unpack it the download. There's a Sencha Touch 2 download, but there's also a SDK Tools download. Additionally, a full working demo can be found here. I eventually figured out that the latter made it the easiest. The magic command was this: sencha generate app example-app ../example-app
That started a new project with all the right files and folders in the right places.From there, I was off to the races.
app.js and app.json: the heart of your Sencha Touch 2 app
This script creates a few files, which do various things. I found myself spending most of my time in two files: app.js and app.json. app.js is the main entry point for the app, and app.json is the app configuration file. Let's start with app.json. This is where you tell your Sencha App about all the resources it needs. I created a `resources/js` folder, dropped in the pubnub javascript library, and then added it to the top of the list like so:
"js": [
{
"path": "resources/js/pubnub-3.1.min.js"
},
{
"path": "../sencha-sdk/sencha-touch.js"
},
{
"path": "app.js",
"update": "delta"
},
],//<debug>
Ext.Loader.setPath({
'Ext': '../sencha-sdk/src'
});
//</debug>
Ext.application({
"name": "PubNub-SenchaTouch",
// Setup your icon and startup screens
phoneStartupScreen: 'resources/loading/Homescreen.jpg',
tabletStartupScreen: 'resources/loading/Homescreen~ipad.jpg',
glossOnIcon: false,
icon: {
57: 'resources/icons/icon.png',
72: 'resources/icons/icon@72.png',
114: 'resources/icons/icon@2x.png',
144: 'resources/icons/icon@114.png'
},
// Require any components we will use in our example
requires: [
'Ext.field.Text',
'Ext.field.Search',
'Ext.field.Select',
'Ext.Button',
'Ext.List',
'Ext.Img'
],
launch: function() {
//... a few things omitted, see below
Ext.create('Ext.Container', {
fullscreen: true,
layout: 'vbox',
items: [
{
xtype: 'toolbar',
flex: 1.5,
items: [
{
xtype: 'spacer'
},
{
xtype : 'panel',
html : '<img style="height:25px; margin-top:15px; margin-bottom:15px;" src="https://pubnub.s3.amazonaws.com/2012/pubnub-large.png">',
height : 60
},
{
xtype : 'panel',
html : '<img style="height:60px;" src="http://www.theberryfix.com/wp-content/uploads/sencha_logo.png">',
},
{
xtype: 'spacer'
}
]
},
{
xtype: 'panel',
layout: 'fit',
flex: 7,
items: [
messageList,
]
},
{
xtype: 'toolbar',
flex: 1.5,
items: [
nameField,
chatField,
submitButton,
]
}
]
});Within the bottom toolbar, I added three interactive elements. A text field for name entry, a text field for the chat message itself, and a submit button. Here's they are defined.
var chatField, nameField, submitButton, messageList;
// Create a text field with a name and placeholder
chatField = Ext.create('Ext.field.Text', {
name: 'chat_input',
id: 'chat_input',
placeHolder: 'type chat here',
flex: 7,
listeners: {
action: sendMessage,
focus: function() {
messageList.getScrollable().getScroller().scrollToEnd();
}
}
});
nameField = Ext.create('Ext.field.Text', {
name: 'name_input',
id: 'name_input',
placeHolder: 'your name',
flex: 2
});
submitButton = Ext.create('Ext.Button', {
iconCls: 'reply',
iconMask: true,
text: 'Send',
ui: 'confirm',
flex: 1,
listeners: {
tap: sendMessage
}
});This is pretty straight forward. I give them names, ids, placeholder text, etc. Layout-wise, again I'm using the `flex` element to define how much space they take up in relation to each other. The most important part here is that `listeners` field. This is where I tell the chat field and submit button to listen for “return” and “tap” events, respectively, and point them at a callback function called sendMessage. That callback is where PubNub comes into play.
Enter PubNub
First, lets initialize PubNub using the init() function. 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.
var pubnub = PUBNUB.init({
publish_key : 'PubNub Publish Key Here',
subscribe_key : 'PubNub Subscribe Key Here',
ssl : false,
origin : 'pubsub.pubnub.com'
});And here's how we send messages:
function sendMessage() {
pubnub.publish({
channel : 'sencha_chat',
message : {
name : "chat_message",
data : {
message : Ext.getCmp('chat_input').getValue(),
user : (Ext.getCmp('name_input').getValue() || "nobody")
}
},
callback : function() {
Ext.getCmp('chat_input').setValue(''); // clear whatever you just typed from the chat box
Ext.getCmp('chat_input').focus(); // keep the focus on the chat box
}
});
}function scrollToBottom() {
messageList.getScrollable().getScroller().scrollToEnd();
}
pubnub.subscribe({
channel : 'sencha_chat',
callback : function(message) {
if (message.name && (message.name == 'chat_message')) {
messageList.getStore().add({
user: (message.data.user || nobody),
message: message.data.message
});
scrollToBottom();
setTimeout( function() {
scrollToBottom();
}, 10);
setTimeout( function() {
scrollToBottom();
}, 50);
}
}
});// preload prev chats
pubnub.history({
channel : 'sencha_chat',
limit : 10
}, function(messages) {
messageList.getStore().removeAt(0); //remove dummy
console.log(messages);
for (m in messages) {
var message = messages[m];
messageList.getStore().add({
user: (message.data.user || nobody),
message: message.data.message
});
}
setTimeout( function() {
scrollToBottom();
}, 100);
});
- See more at: http://www.pubnub.com/blog/building-a-pubnub-powered-chat-app-with-sencha-touch-2/#sthash.v95YOoOt.dpuf