This post originated from an RSS feed registered with Ruby Buzz
by Guy Naor.
Original Post: Ruby and XMPP/Jabber Part 2: Logging in and sending simple messages
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
In this part I will show how to log in to a Jabber server and send a simple message. This is the basis of everything else, and will give you something nice to start with. After all, sending messages is the most common use for Jabber.
Installation
Get the code from here, unpack it and then run ./setup.rb from the directory you open the archive to. YOu can also get the gem and install it locally. As it's pure ruby, there aren't any complications with it.
What we will need?
To start, get irb going as we'll do it interactively so we can get immediate results. You will also need a Jabber account you can login to, and another account to send messages to. You could send to the same account, actually, but it's nicer with a second one.
For this session, I will be using my account at DreamHost - they provide Jabber as one of the features of the account, which is pretty cool. YOu can also setup one of your own, but it's beyons the scope of this post.
Server: yeush.com
User: test
Password: test (don't worry, I'm not this crazy, this is a fake password...)
And we'll interact with another accounts (that has the test account as a buddy that can connect to): test_with_me@yeush.com.
Now in irb, make sure we have xmpp4r included:
irbrequire'xmpp4r/client'includeJabber# Makes using it a bit easier as we don't need to prepend Jabber:: to everything
Logging in
To start any communication with a Jabber server, we need to first log in to the server:
# Jabber::debug = true # Uncomment this if you want to see what's being sent and received!jid=JID::new('test@yeush.com/Testing')password='test'cl=Client::new(jid)cl.connectcl.auth(password)
That's all it takes to login, really! Now for some explanations. The jabber system is made of a collection of servers that can be either stand alone, or connected to each other (called open federation). Each entity connecting to the network must have a unique ID, called a JID (Jabber ID). The id is made up of the user name, server the user is on and an optional resource. The resource is the part after the /, in this case /Testing. The resources can be arbitrarily named, and allow the same person/entity to be connected and talking from many places at once, while still knowing where to send replies back to.
Now that we are connected, let's do something with it...
Sending a simple message
Most of what we want to do in a Jabber session is send messages. So let's get one going:
to="test_with_me@yeush.com"subject="XMPP4R test"body="Hi, this is my first try from XMPP4R!!!"m=Message::new(to,body).set_type(:normal).set_id('1').set_subject(subject)cl.sendm
What did we have here? We assigned a few parameters, created a new message object, and had the client send it. subject is mostly ignored by clients. Very few will show it. body is the plain text message you want to send. *to* is well, the to.
A message is simple an REXML document, and the calls to setid('1') and settype(:normal), set those values in the message element. :normal is the message type. Look in the documentation for all possible types. We will be using :normal and :chat. id is used to identify the message, and can be anything. Usually it's an advancing counter, and it can also include letters and underscore.
After we have the message constructed, we let the client send it. Like I explained in the first part, the whole XMPP protocol is based on XML documents being sent and received. You can type m.to_s in irb to get the document representation. Here is how the message looks in XML:
<message type='normal' id='1' to='test_with_me@yeush.com'>
<body>Hi, this is my first try from XMPP4R!!!</body>
<subject>XMPP4R test</subject>
</message>
Go and play with that a bit, and on the next installment we'll see how to add rich-text to the messages (hint: adding it to the body won't work - you need a special html element for that).