layout: post title: "XMPP4R: Get client's complete roster the right way" date: 2013-03-25 00:28 comments: true categories: [ruby, jabber] cover: /images/cover/avatar.png keywords: ruby, jabber, xmpp, xmpp4r, roster, client, xmpp4r-simple
Connect to Jabber. With XMPP4R, a library for Ruby, it is possible, but not as easy as you could think.
One could ask "Why don't you use xmpp4r-simple?". My answer: "I'm not a pussy!".
There are many little bastards you should know before fetching client's roster with contacts' vcard (nickname, full name, avatar, ...), status (online, away, ...) or status message. This is how I do this task. It works flawlessly.
This is simplified code I use in my chat application. Reading comments might be helpful.
{% codeblock lang:ruby %}
Jabber::debug = true
client = Jabber::Client.new("someuser@somejabberserver.tld") client.connect() client.auth("my password")
roster = Jabber::Roster::Helper.new(client)
roster.getroster() roster.waitfor_roster()
roster.items.each do |jid, contact| puts "In roster I have: " + jid end
roster.addpresencecallback do |rosteritem, oldpresence, newpresence| puts "Somebody changed his/her state to " + newpresence.status.tos # newpresence also offers status message and other stuff end
client.send(Jabber::Presence.new.set_type(:available))
roster.items.each do |jid, contact| # According to documentation, fetching vcard can take longer time. # Use threads if you think it is a good idea. Thread.new do vcard = Jabber::Vcard::Helper.get(client, jid) nickname = vcard && vcard["FN"] || '' #get avatar or other information here end end {% endcodeblock %}
What I've learned from using XMPP4R library in my project -- callbacks are good thing and use them.