julek5119
julek5119

Reputation: 41

Fastest way to get Name, Port and IP of devices in local network using Node.js

I am currently working on a React Native app in which I need to search for devices on my local network to then establish a TCP connection with the identified devices using the node-net library. For this, I require the device's name, port, and IP address. To achieve this, I am currently using the multicast-dns library. However, the identification of the name, port, and IP appears to be really slow, which is not very user-friendly. I would appreciate some suggestions on how to speed up the process or, alternatively, other ways to retrieve the required properties of devices.

import mdns from "multicast-dns";

const mdnsInstance = mdns();

mdnsInstance.on("response", function (response) {
  response.answers.forEach((answer) => {
    if (answer.type === "PTR") {
      console.log(`Service Name: ${answer.name}`);
    } else if (answer.type === "SRV") {
      console.log(`Service Port: ${answer.data.port}`);
    } else if (answer.type === "A") {
      console.log(`Service IP: ${answer.data}`);
    }
  });
});

mdnsInstance.query({
  questions: [
    {
      name: "_services._dns-sd._udp.local",
      type: "PTR",
    },
  ],
});

Upvotes: 2

Views: 58

Answers (0)

Related Questions