Subscribing to real-time data
For this exercise we will be subscribing to a real-time feed from Twitter that is being pushed through the Kwwika service. This feed can be subscribed to on the/KWWIKA/TWITTER/HASHTAGS/TECH topic and contains all real-time Tweets containing the hashtag #tech
SubscribeSubscribe to the topic /KWWIKA/TWITTER/HASHTAGS/TECH using the kwwika.Connection.subscribe method. Pass in a subscription listener to receive updates on the subscribed topic.
Handle updatesFrom the topicUpdated callback build a string that represents some HTML you would like to add ot the web page.
There are a number of fields available on the mUpdate parameter (map object):
Heres an example mUpdate object for /KWWIKA/TWITTER/HASHTAGS/TECH in object literal notation:
({InReplyToScreenName:"techcores", CreatedAt:"5/15/2010 6:17:11 PM", Truncated:"false", Text:"#tech Auto-dimming electrochromic panels reduce glare when driving (video) http://ow.ly/17nTwo", ScreenName:"justindnewton", UserFollowersCount:"3318", Favourited:"false", TotalTweets:"112", RetweetTotal:"16", UserName:"Justin Newton", UserProfileImageUrl:"http://a1.twimg.com/profile_images/900568752/justinnewtonimg_normal.jpg", InReplyToUserId:"88088839", Id:"14048836922", InReplyToStatusId:"=", Source:"<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</a>"})
Fields can be accessed in two ways :
var sText = mUpdate["Text"] ; var sText2 = mUpdate.Text;
Update the web pageSo, for this exercise, if you've created a div with an id "latest_div" you could do the following:
document.getElementById("latest_div").innerHTML = mUpdate.Text;
Hints & Tips
Subscribing// Define a subscription listener object. var oSubscriptionListener = function() { this.topicUpdated = function(oSub, mUpdate) { // handle update }; this.topicError = function(oSubscription, sReason) { // handle errors } } var oSub = oConn.subscribe("/KWWIKA/TWITTER/HASHTAGS/TECH", oSubscriptionListener);
oConn is the connection object you got in Exercise 1 when you connected. You'll notice that you pass the topic you want to subscribe to and a subscription listener object into the subscribe method.
The next exercise is Exercise 3: More subscribing. |
