Serverless Messaging using Bonjour/DNSDS/mDNS, XMPP, and Ruby

34
1
Ruby

=Ahoy
Serverless Messaging using Bonjour/DNSDS/mDNS, XMPP, and Ruby

Wha?

In simpler terms, this is a library for sending messages to iChat (or Adium, Pidgin, etc) using the Bonjour chat protocol.

The Bonjour chat protocol is pretty much XMPP with the presence and server parts ripped out and replaced Bonjour (DNS Service Discovery and multicast DNS). Like standard XMPP while mainly used as for instant messaging, there is nothing stopping you for using it for a more generic messaging system, a presence system or interprocess communication.

Ahoy isn’t much more than a wrapper with a nice API around the dnssd and xmpp4r gems.

Example:

require ‘rubygems’
require ‘ahoy’

user = Ahoy::User.new(“mat”)
user.sign_in

sleep 1 # wait for the contact list to populate

first_contact = user.contacts.first # find someone to talk to

chat = user.chat(first_contact) # open a chat with them

chat.send(“We come in peace.”) # send a message

puts chat.receive # blocks until we get a response

chat.close

Along with the blocking Chat#receive method to receive replies, there is an on_reply method that takes a callback. This method returns immediately, and the block is only called when there is a reply, and can be used like so

require ‘rubygems’
require ‘ahoy’

user = Ahoy::User.new(“mat”)
user.sign_in
sleep 1
chat = user.chat(user.contacts.first)

chat.on_reply do |reply|
puts reply
end

chat.send(“hello”)

do something while we wait for a reply

loop do
sleep 1
end

Messages can be formatted using markdown, simply set Chat#use_markdown (or Ahoy.use_markdown for the global default). Ahoy will use any of the following markdown processors, in order of preference: rdiscount, kramdown, maruku, bluecloth.

The current use case is to send a message to a team of developers when a deploy script is run, there is a simplified interface for this:

require ‘rubygems’
require ‘ahoy/broadcast’

user = Ahoy::User.new(“Dr. Nick”)
cast = Ahoy::Broadcast.new(user)

cast.send(“Hi, everybody!”)
cast.close