lib/registry.js
function probe (mdns, service, cb) {
  var sent = false
  var retries = 0
  var timer

  mdns.on('response', onresponse)
  setTimeout(send, Math.random() * 250)

  function send () {
    // abort if the service have or is being stopped in the meantime
    if (!service._activated || service._destroyed) return

    mdns.query(service.fqdn, 'ANY', function () {
      // This function will optionally be called with an error object. We'll
      // just silently ignore it and retry as we normally would
      sent = true
      timer = setTimeout(++retries < 3 ? send : done, 250)
      timer.unref()
    })
  }

  function onresponse (packet) {
    // Apparently conflicting Multicast DNS responses received *before*
    // the first probe packet is sent MUST be silently ignored (see
    // discussion of stale probe packets in RFC 6762 Section 8.2,
    // "Simultaneous Probe Tiebreaking" at
    // https://tools.ietf.org/html/rfc6762#section-8.2
    if (!sent) return

    if (packet.answers.some(matchRR) || packet.additionals.some(matchRR)) done(true)
  }

  function matchRR (rr) {
    return dnsEqual(rr.name, service.fqdn)
  }

  function done (exists) {
    mdns.removeListener('response', onresponse)
    clearTimeout(timer)
    cb(!!exists)
  }
}
function probe (mdns, service, cb) {
  var sent = false
  var retries = 0
  var timer

  mdns.on('response', onresponse)
  setTimeout(send, Math.random() * 250)

  function send () {
    // abort if the service have or is being stopped in the meantime
    if (!service._activated || service._destroyed) return

    mdns.query(service.fqdn, 'ANY', function () {
      // This function will optionally be called with an error object. We'll
      // just silently ignore it and retry as we normally would
      sent = true
      timer = setTimeout(++retries < 3 ? send : done, 250)
      timer.unref()
    })
  }

  function onresponse (packet) {
    // Apparently conflicting Multicast DNS responses received *before*
    // the first probe packet is sent MUST be silently ignored (see
    // discussion of stale probe packets in RFC 6762 Section 8.2,
    // "Simultaneous Probe Tiebreaking" at
    // https://tools.ietf.org/html/rfc6762#section-8.2
    if (!sent) return

    if (packet.answers.some(matchRR) || packet.additionals.some(matchRR)) done(true)
  }

  function matchRR (rr) {
    return dnsEqual(rr.name, service.fqdn)
  }

  function done (exists) {
    mdns.removeListener('response', onresponse)
    clearTimeout(timer)
    cb(!!exists)
  }
}
function probe ( mdns , service , cb ) { var sent = false var retries = 0 var timer mdns . on ( 'response' , onresponse ) setTimeout ( send , Math . random ( ) * 250 ) function send ( ) { // abort if the service have or is being stopped in the meantime if ( ! service . _activated || service . _destroyed ) return mdns . query ( service . fqdn , 'ANY' , function ( ) { // This function will optionally be called with an error object. We'll // just silently ignore it and retry as we normally would sent = true timer = setTimeout ( ++ retries < 3 ? send : done , 250 ) timer . unref ( ) } ) } function onresponse ( packet ) { // Apparently conflicting Multicast DNS responses received *before* // the first probe packet is sent MUST be silently ignored (see // discussion of stale probe packets in RFC 6762 Section 8.2, // "Simultaneous Probe Tiebreaking" at // https://tools.ietf.org/html/rfc6762#section-8.2 if ( ! sent ) return if ( packet . answers . some ( matchRR ) || packet . additionals . some ( matchRR ) ) done ( true ) } function matchRR ( rr ) { return dnsEqual ( rr . name , service . fqdn ) } function done ( exists ) { mdns . removeListener ( 'response' , onresponse ) clearTimeout ( timer ) cb ( ! ! exists ) } }
Check if a service name is already in use on the network.

Used before announcing the new service.

To guard against race conditions where multiple services are started
simultaneously on the network, wait a random amount of time (between
0 and 250 ms) before probing.

TODO: Add support for Simultaneous Probe Tiebreaking:
https://tools.ietf.org/html/rfc6762#section-8.2
Check if a service name is already in use on the network .