Hello The Speed Bump

Or rather, bonjour le bompe de vitesse, or, er, something. What am I blathering about? Okay, let’s say you were going to use Bonjour (aka The Technology Formerly Known As Rendezvous, aka zeroconf) to do some HTTP, you might be tempted to simply take the Bonjour Hostname, stick it in a URL, and use it like that. For instance, if your Bonjour Service Browser finds Wooji.local. you might be tempted to fetch http://Wooji.local./some/url.html.

Don’t.

Because if you do that, every single URL-fetch will re-resolve the hostname into its IP address over multicast DNS which, on my machines, seems to add about 2 seconds to the roundtrip time. Yuck.

You should instead resolve the service with resolveWithTimeout(), and in the netServiceDidResolveAddress: callback, get the service’s addresses(), and use one of those (there’ll probably be both IPv4 and IPv6 included in the array) in your URL.

Now, caching the result of resolveWithTimeout() is somewhat of a grey area — Apple recommend against it, but they’re assuming a connection-based model where you typically stay connected for a long time. If this doesn’t suit your networking model, it’s probably best to do something with a cache and a timeout — it’s a bit… messy… but it should work.

Make sure to clear it from your cache if the service goes offline (if you receive netServiceBrowser:didRemoveService:moreComing:).