preload
Jun 17

On my last post I gave you a very basic introduction to XMPP. Now, since I know it was a big snoozer, lets get to the good stuff: the code. On this post you will find a simple Ruby script that lets you use an IM (Instant Messaging) client to update your Twitter account by leveraging XMPP and the XMPP4R gem.

Things you will need:


  • Jabber server
  • IM client
  • XMPP4R
  • Twitter account
  • Twitter gem

Jabber server
There are several Jabber servers that you can download and install for free. Two of the most common ones are Openfire and ejabbered.

Once you have the server installed please create two users. For the purpose of this tutorial one of these users will be a bot. I called mine localbot.

What is a bot?
A bot is just a account on the Jabber server that will be used to perform actions or services. It is not controlled by another human.

IM client:
I use iChat as my client, but again there are many other really good products out there that you can use. Take a look at Pidgin or Psi.

Once you have your IM client installed, please connect to the Jabber server with both of the user accounts you created during the server installation. Finally make sure you add each of the newly created accounts as as a contact for one and another.

Why do we have to make them buddies/contacts?
Simply put, if they are not contacts, they won’t be able to send each other messages. This prevents unauthorized users from sending spam.

XMPP4R:
As the XMPP4R team describe it,

“XMPP4R is an XMPP/Jabber library for Ruby. Its goal is to provide a complete framework to develop Jabber-related applications or scripts in Ruby.”

Their site has several different ways for installing this library. If you want to use RubyGems, simply type in your terminal:

gem install xmpp4r

Twitter:
Really? You don’t have a Twitter account? Go ahead get an account and add me!

Twitter gem:
To install with RubyGems type in your terminal:

gem install twitter

Just give me the code:


1        #!/bin/env ruby
2        require ‘rubygems’
3        require ‘xmpp4r’
4        require ‘twitter’
5
6        #Set to true of you want to see the logs
7        Jabber::debug = true
8
9        #Log into Jabber server with your bot
10        jid = Jabber::JID.new(”localbot@box.local/twitter”)
11        client = Jabber::Client.new(jid)
12        client.connect
13        client.auth(”password”)
14
15        #Announce your presence to your contacts
16        client.send(Jabber::Presence.new.set_status(”What you doing right now?”))
17
18        #Listen for incoming messages and process them
19        client.add_message_callback do |t|
20            puts “Got a message need to tweet it!”
21             httpauth = Twitter::HTTPAuth.new(’username’, ‘password’)
22            base = Twitter::Base.new(httpauth)
23            puts t.body.to_s
24            base.update(t.body)
25        end
26
27        Thread.stop

Wait, what? Explain the code:


Lets start at line 7. This line is simply asking if you want to print out the XMPP logs on the terminal. I have it set to true but you might want to set it to false since it can be overwhelming.

The next section to discuss includes lines 10 through 13. This is the code that will connect a user or account to the Jabber server. Line 10 is creating a JID which stands for JabberID. This JID is essentially the user’s identity on the network. In my script I am passing in localbot@box.local/twitter and as you can tell it looks very similar to an email address except for that /twitter section at the end. This is called the resource identifier and it is used to identify a specific connection by the user. Please replace my full JID with your own full JID. The resource identifier can be called anything.

The JID structure:
node @ domain / resource

Bare vs Full JID:
bare JID: localbot@box.local
full JID: localbot@box.local/twitter

Lines 11 and 12 create the connection with the Jabber server. At this point we should all take some time to thank the XMPP4R team for making our lives so simple. As to be expected, there is a lot more magic happening in the background. First of all, the XMPP4R library is negotiating an “always on” connection. This connection will allow for the negotiation and creation of the XML stream. You can image this stream as a XML document that just gets bigger as communication between the client and server goes back and forth. The XML stream, plus the always on connection are some of the reasons why XMPP communication is instant.

Lets look at this a different way:

1. First the connection is made.

XMPP connection

XMPP connection

2. The XML stream is negotiated and opened from the client to the server. Another stream is also created from the server to the client.

xmpp_open_streams

Once the negotiation is complete, the client and the server will only be exchanging a variation of XML stanzas. There are three main XML stanzas that are passed and these are, <message>, <presence> and <iq>.

xmpp_stanzas

On line 16 we send our first XML stanza. If you run the script, you will notice that the status of your bot will reflect whatever the string is that you are passing in the set_status method. In my case it says What you doing right now? This is the presence stanza.

Presence is the way we can tell our contacts about our availability on the network. At the start of this tutorial you were asked to manually subscribe each user to one and another. This was done so that each user could see each other’s presence. The process of authorizing users to see each other’s presence is called presence subscription.

Here is a sample of a presence stanza:

<presence xmlns=’jabber:client’>
<show>chat</show>
<status>What you doing right now?</status>
</presence>

You can pass different values in the show element to change your presence status:
1. away = Your are away.
2. chat = Ready to chat
3. dnd = Don’t bother
4. xa = Extended away
5. if you dont include a <show>, then it means you are available

Lines 19 to 25 sets a message call back. XMPP4R has several of these callback methods that essentially sit and wait for something to happen. In this case when the script is ran, a message callback is set. Now, if I send a message, or a XML stanza of type <message>, from my client to my localbot account, the add_message_callback will catch that message and process it. In this case it will use the Twitter library to update my Twitter status (Lines 21 – 24).

Sample from the logs of my message stanza:

<message from=’guille@box.local/K-TI’ type=’chat’ to=’localbot@box.local’>
<body>This is my message that I typed in from iChat</body>
</message>

Notice the type attribute. Message stanzas can have several different values:
chat = If for real time communication, as in a chat!
normal = This is more like an email. In this case a response is not necessarily expected.
groupchat = This is used in for chat room with many users.
headline = Headlines are used for notifications were a response is not expected.
error = When a problem occurs, the device that catches the error will return a error message to the device that sent the message.

The third of the XML stanzas is <iq> or Info/Query stanza. This is very similar to HTTP in that it gives us a way of performing request and response actions through XMPP. I will talk more about these stanzas in my next XMPP post.

Line 27, although not XMPP or XMPP4R related is important to this script. Without it, the script would just continue on to the next line and finish the script.

So there you go, a simple script that lets you send messages from your IM client to your Twitter account. Plus now you know the basics of XMPP and how the servers communicate.

Other resources:

1. XMPP: The Definitive Guide

2. NFJS, The Magazine – Check out Brian Sletten’s article titled XMPP for the People: Openfire and the Smack API

  • Share/Save/Bookmark
May 19

xmppI decided to look into XMPP after the last NFJS in Reston, VA. It was a lecture by Brian Sletten that made me realize how powerful this technology could be. Before the lecture my only knowledge of this protocol was that it has something to do with instant messaging. After the lecture I was excited and motivated to look deeper into this protocol. My goal with this post is to give you a general idea of what this protocol is and what it could be used for. As I experiment with different libraries, I will post code examples of how XMPP could be used on your web application.

What is XMPP?
XMPP stands for Extensible Messaging and Presence Protocol. The xmpp.org site defines it as “a set of open XML technologies for presence and real-time communication.” In other words, this is a technology that allows you to send XML from one place to another almost instantly.

How does it work?
As an architecture, XMPP is similar to email in that, not only do they both use a client-server structure, but also, all the different servers are connected to allow cross-domain communication. In essence, just like a user with a @mac address can send a email message to a @gmail account, so can the @gmail user send a XMPP message to a @mac account.

The big difference between email and XMPP is how the connected servers will communicate when a message is being sent. On an email system, the client will connect to the server which will then decide where to forward the message too. This message could be forwarded to multiple servers before it reaches it’s destination. On a XMPP architecture, the client will connect to the Jabber server which will connect directly to the recipient’s server and deliver the message.

To XMPP?
XMPP can be used for many different reasons. Of course we can use it for instant messaging, but what else? One use that seems to be gaining popularity is notifications. Most of the current applications are using RSS like, pull technologies, wasting bandwidth and server resources by having to go and check every so often for new messages (even when there aren’t any new messages to get pulled). Considering that XMPP is a push technology, notifications will be sent instantly when a new message is generated, there is no need to check for it.

Another great use for XMPP could be data transfer. A simple example of this would be an application that allows you to update your twitter account from your instant messaging client.

There are many other things that can be done including, monitoring systems, identifying the presence of users, and even controlling servers from our instant messaging client. In the end its up to our imagination.

Or NOT XMPP?
The biggest reason for not using XMPP is, if you need to guarantee that a user will see the message. With a XMPP architecture, if a recipient’s client application is not running, it is possible that the message will never get to him/her.

If you are interested in XMPP keep checking back as I will be posting tutorials with code examples. If you are still confused on the power of XMPP, check out Google Wave, its somewhat of a cool application :)

  • Share/Save/Bookmark
Tagged with:
May 05

motivatorcube1During the last NFJS conference I attended, Neal Ford gave a speech about how the environment can have a serious effect on how productive we are. He mentioned that we love programming because it is a way for us to reach the “Flow”. The concept of the “Flow” was proposed by Mihály Csíkszentmihályi, and is defined as “the mental state of operation in which the person is fully immersed in what he or she is doing by a feeling of energized focus, full involvement, and success in the process of the activity.”

Neal, gave us a list of good ideas one how we can reach our “Flow”, after all this is not something we can just turn on and off. The list included, comfortable chairs, double monitors, toys and NO CUBE FARMS. He also talked about the difference between the left and right side of our brains and how we need both for our line of work.

For me, as well as many others, I believe that music is a good way of reaching the “Flow”. Here is a list of some of my favorite artist to work with:

1. Brian Eno

2. E.A.R. (Experimental Audio Research)

3. Flying Saucer Attack

4. Can

5. Kraftwerk

6. Leonard Cohen

7. Mojave 3

8. Nick Drake

9. Sigur Ros

10. Tom Waits

  • Share/Save/Bookmark
May 02

iphone

Just curious, with all the rumors going around that we might see an iPhone for Verizon sometime next year (2010), would AT&T customers switch to Verizon? I am a Sprint customer ( ugh! ) and one of things stopping me from getting the iPhone is AT&T ( the other is the monthly plan ).

If news comes out that Verizon is getting a iPhone I will switch immediately.

you?

  • Share/Save/Bookmark
May 02

groovyrecipesSimply put this book will save you a lot of time.

The Groovy Recipes book by Scott Davis contains many examples of very common activities that will help anyone!

  • Share/Save/Bookmark