Registering a service using DNSSD and Zeroconf
In this series of posts I’m using the power of Zeroconf and DNSSD to share my strawberry ice cream advocacy web site with the rest of the network. In the last post we learned all about Zeroconf and how to install DNSSD so you can use Zeroconf from Ruby.
In this post I’ll put DNSSD to work and we’ll use it to register our web site on the network.
To begin let’s go back and look at my web site code:
require 'rubygems' require 'sinatra' get '/' do puts "I love Strawberry ice cream!" end
You can run your Sinatra application by saving this file as sp1.rb typing:
ruby sp1.rb
By default Sinatra will run on port 4567. To specify your own port stop the web server by pressing CTRL+C and start it again using:
ruby sp1.rb -p 12345
This will start Sinatra on port 12345, but you can use any port you want.
Now that we know how to run Sinatra we need to register our service using Zeroconf.
I’ll begin by requiring DNSSD. I like to require a specific version since I’ve had trouble compiling older versions of the gem on Linux:
gem 'dnssd', '0.7.1' require 'dnssd'
Now we’ll add some lines right before we start the Sinatra application to register the web server:
register_service = DNSSD.register("strawberry ice cream +1", "_http._tcp", nil, Sinatra::Application.port) do puts "* Registered the strawberry web server" end
It’s good form to keep a reference to the register service around so you can unregister it manually using:
register_service.stopNormally you won’t need to worry about this since DNSSD will unregister the service automatically when the Sinatra application terminates and the register_service object is cleaned up.
The final code should look like this:
require 'rubygems' require 'sinatra' gem 'dnssd', '0.7.1' require 'dnssd' get '/' do "I love strawberry ice cream!" end register_service = DNSSD::register( "Strawberry Ice Cream +1", "_http._tcp.", nil, Sinatra::Application.port) do puts "* Registering the strawberry web server" end
If you’re on a Mac or a PC running Safari and you want to see the website on your browser, just open your Safari Preferences (Safair > Preferences) and then check off the Bonjour boxes under the Bookmarks preferences panel. Close the Preferences pane and you should see a Bonjour folder on your bookmarks bar with an entry for “strawberry ice cream +1″.
Non-Safari users on Ubuntu, Mac OS X, and Windows can follow the directions Andrew TJ’s site to install the BonjourFoxy Firefox addon which will show Zeroconf web servers on your network.

Original Source: http://andrew.tj.id.au/projects/bonjourfoxy/
And that’s it, you’re now sharing your love of strawberry ice cream with the whole network!
In my next post I’ll talk about how to write Ruby code to automatically discover and list other strawberry ice cream lovers on your network.










