fork of zeroconf to apply patches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Gavin Li 401b8b5759 Properly implement TEventLoop::poll for AvahiEventLoop 6 months ago
.github/workflows Run clippy in workflow 10 months ago
examples 0.10.2 10 months ago
scripts Run clippy in workflow 10 months ago
zeroconf Properly implement TEventLoop::poll for AvahiEventLoop 6 months ago
zeroconf-macros Increment versions 2 years ago
.gitignore Add ServiceType 11 months ago
Cargo.lock 0.10.2 10 months ago
Cargo.toml Move from magiclip project 2 years ago
LICENSE Initial commit 2 years ago Fix ServiceType logic 10 months ago


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.


On Linux:

$ sudo apt install xorg-dev libxcb-shape0-dev libxcb-xfixes0-dev clang avahi-daemon libavahi-client-dev


  • Windows support
  • You tell me...


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 =;

    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());

    // ...