Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Using XMPP4R-Simple to check for activity.

Using XMPP4R--Simple these Ruby code snippets show a user's status change and any messages they may have sent you since the last time.

# user1 changes their status to away
jabber.presence_updates do |friend, old_presence, new_presence|
  puts "Received presence update from #{friend.to_s}: #{new_presence}"
end

# user1 sends the message "do you like Tofu?"
jabber.received_messages do |message|
  puts "Received message from #{message.from}: #{message.body}"
end



Reference: http://xmpp4r-simple.rubyforge.org/

Sending a message using XMPP and Ruby

This Ruby code shows how to send an instant message through XMPP [wikipedia.org] using XMPP4R [gna.org]. Source code origin: Ruby and XMPP/Jabber Part 2: Logging in and sending simple messages [famundo.com].

Preparation
- Installed eJabberd on my Gentoo server
- Created 2 user accounts using Pidgin Internet Messenger on my Ubuntu desktop.
- Tested sending messages using Jabber Instant Messaging (Gabber) and Pidgin Internet Messenger.

Installation
gem install xmpp4r

What we will need
require 'rubygems'
require 'xmpp4r'
include Jabber

Logging in
jid = JID::new('yourname@yourdomain.com/Testing')
password = 'yourpassword'
cl = Client::new(jid)
cl.connect
cl.auth(password)

Sending a simple message
to = "user2@yourdomain.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.send m

References:
- Building a Twitter Agent with Ruby and Rails [rubyinside.com]
- http://gentoo-wiki.com/Ejabberd

* update 15:45 18-Feb-08 *
Here's a simpler example I found from Liminal Existence: Announcing Jabber::Simple [romeda.org]

sudo gem install xmpp4r-simple
require 'xmpp4r-simple'

jabber = Jabber::Simple.new('yourname@yourdomain.com', 'yourpassword')
jabber.deliver("user1@yourdomain.com", "Hey! I'm thinking of going Vegetarian - Any suggestions?")


see also: IM Integration With XMPP4r : Part 2 [rubyfleebie.com]
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS