Expand description

zeroconf is a cross-platform library that wraps underlying ZeroConf/mDNS implementations such as Bonjour or Avahi, providing an easy and idiomatic way to both register and browse services.

This crate provides the cross-platform MdnsService and MdnsBrowser available for each supported platform as well as platform-specific modules for lower-level access to the mDNS implementation should that be necessary.

Most users of this crate need only MdnsService and MdnsBrowser.


Register a service

When registering a service, you may optionally pass a “context” to pass state through the callback. The only requirement is that this context implements the Any trait, which most types will automatically. See MdnsService for more information about contexts.

use std::any::Any;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use zeroconf::prelude::*;
use zeroconf::{MdnsService, ServiceRegistration, ServiceType, TxtRecord};

#[derive(Default, Debug)]
pub struct Context {
    service_name: String,

fn main() {
    let mut service = MdnsService::new(ServiceType::new("http", "tcp").unwrap(), 8080);
    let mut txt_record = TxtRecord::new();
    let context: Arc<Mutex<Context>> = Arc::default();

    txt_record.insert("foo", "bar").unwrap();


    let event_loop = service.register().unwrap();

    loop {
        // calling `poll()` will keep this service alive

fn on_service_registered(
    result: zeroconf::Result<ServiceRegistration>,
    context: Option<Arc<dyn Any>>,
) {
    let service = result.unwrap();

    println!("Service registered: {:?}", service);

    let context = context

    context.lock().unwrap().service_name = service.name().clone();

    println!("Context: {:?}", context);

    // ...

Browsing services

use std::any::Any;
use std::sync::Arc;
use std::time::Duration;
use zeroconf::prelude::*;
use zeroconf::{MdnsBrowser, ServiceDiscovery, ServiceType};

fn main() {
    let mut browser = MdnsBrowser::new(ServiceType::new("http", "tcp").unwrap());


    let event_loop = browser.browse_services().unwrap();

    loop {
        // calling `poll()` will keep this browser alive

fn on_service_discovered(
    result: zeroconf::Result<ServiceDiscovery>,
    _context: Option<Arc<dyn Any>>,
) {
    println!("Service discovered: {:?}", result.unwrap());

    // ...


pub use browser::ServiceDiscoveredCallback;
pub use browser::ServiceDiscovery;
pub use service::ServiceRegisteredCallback;
pub use service::ServiceRegistration;


Trait definition for cross-platform browser

Utilities regarding error handling

Trait definition for cross-platform event loop

Linux-specific ZeroConf bindings

Crate prelude

Trait definition for cross-platform service.

TxtRecord utilities common to all platforms


Data type for constructing a service type to register as an mDNS service.


Represents a network interface for mDNS services

Type Definitions

Type alias for the platform-specific structure responsible for polling the mDNS event loop

Type alias for the platform-specific mDNS browser implementation

Type alias for the platform-specific mDNS service implementation

Result type for this library

Type alias for the platform-specific structure responsible for storing and accessing TXT record data