Monday, 14 December 2009

XMPP4R - Using Ruby to send Jabber messages

Logging, as I've recently found out, is critically important. Well yes I've always know that but I had this odd case where my custom CMS that powers this site was falling over when generating RSS feeds. The error_log file in the application root was being populated but well unless you're looking for errors you don't often check that.

Unfortunately I've not come across a working XMPP client for PHP (XMPPHP was erroring out) so Ruby to the rescue. Here's some sample code to send a message. I'll look at integrating this at some point during the holidays so that I can get critical events directed straight through to me. This would be quite a handy scenario especially now that I've got Trillian for the iPhone almost along with it's glorious push notifications!

require 'xmpp4r'
include Jabber

jid = JID::new( '-@0gravity.co.uk' );
password = '-'
cl = Client::new(jid)
cl.connect( 'talk.google.com' )
cl.auth( password )

to = '-@0gravity.co.uk'
subject = 'XMPP4R test'
body = 'Hi, this is a XMPP4R test'
m = Message::new( to, body ).set_type(:chat).set_id('1').set_subject(subject)
cl.send m

Useful links:

  • IM Integration With XMPP4r part 1 / part 2
  • Ruby and XMPP/Jabber - a Multi Posts Series part 1, part 2 and part 3
  • And of course see here where they nicely remind you to run setup.rb ;-)

The following pages link here:


Zend_Log and custom writer Log_Writer_Script