How to use the mdns.dns_sd function in mdns

To help you get started, we’ve selected a few mdns examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gebhardm / energyhacks / Graph / serve_mqtt.js View on Github external
* ************************************************************
 * Note: see package.json for dependencies
 */
// use http for page serving, fs for getting the *.html files
var httpport = 1080;

// use mqtt for client, socket.io for push,
var mqtt = require("mqtt");

// specify your MQTT broker's data here
// var mqttbroker = "192.168.0.50", mqttport = "1883";
// detect mqtt server via mdns
var mdns = require("mdns");

// resolution requence added due to mdns issue - see https://github.com/agnat/node_mdns/issues/130
var sequence = [ mdns.rst.DNSServiceResolve(), "DNSServiceGetAddrInfo" in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({
    families: [ 4 ]
}), mdns.rst.makeAddressesUnique() ];

// detect mqtt publishers and create corresponding servers
var mdnsbrowser = mdns.createBrowser(mdns.tcp("mqtt"), {
    resolverSequence: sequence
});

// handle detected devices
mdnsbrowser.on("serviceUp", function(service) {
    console.log("Detected MQTT service on: " + service.addresses[0] + ":" + service.port);
    mqttconnect(service.addresses[0], service.port);
});

// handle if mdns service goes offline
mdnsbrowser.on("serviceDown", function(service) {
github gebhardm / energyhacks / Gauge / serve_mqtt.js View on Github external
// define the http server
var http = require("http").createServer(httphandler).listen(httpport);

var fs = require("fs");

var url = require("url");

var path = require("path");

var io = require("socket.io")(http);

// multicast DNS service discovery
var mdns = require("mdns");

// resolution requence added due to mdns issue - see https://github.com/agnat/node_mdns/issues/130
var sequence = [ mdns.rst.DNSServiceResolve(), "DNSServiceGetAddrInfo" in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({
    families: [ 4 ]
}), mdns.rst.makeAddressesUnique() ];

// detect mqtt publishers and create corresponding servers
var mdnsbrowser = mdns.createBrowser(mdns.tcp("mqtt"), {
    resolverSequence: sequence
});

mdnsbrowser.on("serviceUp", function(service) {
    console.log("Detected MQTT service on: " + service.addresses[0] + ":" + service.port);
    mqttconnect(service.addresses[0], service.port);
});

mdnsbrowser.on("serviceDown", function(service) {
    console.log("MQTT service down: ", service);
});
github superhawk610 / multicast / app / lib / devices.js View on Github external
const findDevices = () => {
    /* Look for mDNS Cast devices on local network */
    let browser = mdns.createBrowser(mdns.tcp('googlecast'))

    /* Only scan IPv4 addresses */
    mdns.Browser.defaultResolverSequence[1] =
      'DNSServiceGetAddrInfo' in mdns.dns_sd
        ? mdns.rst.DNSServiceGetAddrInfo()
        : mdns.rst.getaddrinfo({ families: [4] })

    browser.on('serviceUp', service => {
      // service.name: Chromecast-hexadecimalid
      let _id = service.name.split('-'),
        id = _id.pop()

      // fix for groups with IDs that resemble Google-Cast-Group-{deviceId}-1
      while (_id.length > 0 && id.length < 3) id = _id.pop()

      /* If device is registered, append local statistics */
      if (func.isRegistered(id)) {
        func.update(id, {
          name: service.txtRecord.fn,
          address: service.addresses[0],
github NorthernMan54 / homebridge-alexa / lib / HAPInterface.js View on Github external
function HAPDiscovery(options) {
  pin = options.pin;
  debug("Starting Homebridge Discovery");
  try {

    var sequence = [
      mdns.rst.DNSServiceResolve(),
      'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({
        families: [4]
      }),
      mdns.rst.makeAddressesUnique()
    ];
    var browser = mdns.createBrowser(mdns.tcp('hap'), {
      resolverSequence: sequence
    });
    browser.on('serviceUp', function(service) {
//      debug("Found HAP device: %s", service.name);
      const dns = require('dns');
      const options = {
        family: 4,
        hints: dns.ADDRCONFIG | dns.V4MAPPED,
      };
      dns.lookup(service.host, options, (err, hostname, family) => {
        _getAccessories(hostname, service.port, service.name, function(err, data) {
github node-red / node-red-nodes / io / mdns / mdns.js View on Github external
var mdns = require('mdns');
        if (process.platform === "linux") {
            RED.log.info("You may ignore the warning about Bonjour compatability.");
        }
        RED.nodes.createNode(this, n);
        this.topic = n.topic || "";
        this.service = n.service;
        var node = this;
        // var sequence = [
        //     mdns.rst.DNSServiceResolve(),
        //     mdns.rst.getaddrinfo({families: [4] })
        // ];
        // var browser = mdns.createBrowser(this.service,{resolverSequence: sequence});
        var sequence = [
            mdns.rst.DNSServiceResolve(),
            'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({families:[4]}),
            mdns.rst.makeAddressesUnique()
        ];
        var browser = mdns.createBrowser((this.service), {resolverSequence:sequence});

        browser.on('serviceUp', function(service) {
            if (RED.settings.verbose) { node.log("here : " + service.name); }
            service.state = true;
            var msg = {topic:node.topic, payload:service};
            node.send(msg);
        });
        browser.on('serviceDown', function(service) {
            if (RED.settings.verbose) { node.log("away : " + service.name); }
            service.state = false;
            var msg = {topic:node.topic, payload:service};
            node.send(msg);
        });
github vincentbernat / nodecastor / lib / scanner.js View on Github external
function Scanner(options) {
  var self = this,
      family = (options || {}).family || 0, // UNSPEC, can also use 4 or 6.
      sequence = [
        mdns.rst.DNSServiceResolve(),
        'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({families:[family]}),
        mdns.rst.makeAddressesUnique()
      ];
  events.EventEmitter.call(this);
  options = options || {};
  this.logger = options.logger || logger.devnull;
  this.timeout = options.timeout || 5000;
  this.reconnect = options.reconnect || {};
  this.browser = mdns.createBrowser(mdns.tcp('googlecast'),
                                    {resolverSequence: sequence});
  this.browser
    .on('serviceUp', onServiceUp)
    .on('serviceDown', onServiceDown);
  this.devices = [];

  function onServiceUp(device) {
    if (!device.addresses ||
github artificialhoney / aircast / lib / discovery.js View on Github external
Discovery.prototype._init = function () {
  var self = this

  var sequence = [
    mdns.rst.DNSServiceResolve(),
    'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({families: [4]}),
    mdns.rst.makeAddressesUnique()
  ]

  this.browser = mdns.createBrowser(mdns.tcp('raop'), {resolverSequence: sequence})
  this.browser.on('serviceUp', function (info) {
    self.devices[info.name] = info
    self.emit('serviceUp', info)
    self.logger.info('Device up for service: %s', info.name)
    self.logger.debug(info)
  })
  this.browser.on('serviceDown', function (info) {
    var device = self.devices[info.name]
    self.emit('serviceDown', info)
    self.logger.info('Device down for service: %s', info.name)
    self.logger.debug(info)
    if (device) {
github alvarowolfx / gcloud-iot-edge-tensorflow / edge-server / DeviceListener.js View on Github external
constructor() {
    super()
    const sequence = [
      mdns.rst.DNSServiceResolve(),
      'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo( { families : [ 0 ] } ),
      mdns.rst.makeAddressesUnique()
    ]    
    this.browser = mdns.createBrowser( mdns.tcp( 'camera' ), { resolverSequence : sequence } )     
    this.devices = {}
  }