nsd 1.4.4 copy "nsd: ^1.4.4" to clipboard
nsd: ^1.4.4 copied to clipboard

outdated

A Flutter plugin for network service discovery and registration (aka NSD / DNS-SD / Bonjour / mDNS).

nsd #

Platform Tests Android Tests iOS Tests macOS Tests codecov License: MIT

A Flutter plugin for network service discovery and registration (aka NSD / DNS-SD / Bonjour / mDNS).

Service discovery #

import 'package:nsd/nsd.dart';

final discovery = await startDiscovery('_http._tcp');
discovery.addListener(() {
  // discovery.services contains discovered services
});

// ...

await stopDiscovery(discovery);

Service registration #

import 'package:nsd/nsd.dart';

final registration = await register(
  const Service(name: 'Foo', type: '_http._tcp', port: 56000));

// ...

await unregister(registration);

Example application #

The plugin includes an example application that can be used to start multiple discoveries and register multiple services. It will discover its own services but also other services of type _http._tcp in the local network, such as the printer in the screenshot above.

  • Use the action button to add a discovery or register a new service.
  • Swipe the cards left or right to dismiss a discovery or service.
  • The application log will show the calls and callbacks platform side vs. native side.
  • The source code demonstrates how to use the discovery object as a ChangeNotifier.

Get IP addresses for service #

First, do you really need the IP address? If you just want to connect to the service, the host name that is supplied with the service should do just fine. In fact, connecting by host name is recommended on the Apple Developer Forums.

If you do need the IP address, you can configure automatic ip lookup for your discovery like this:

final discovery = await startDiscovery(serviceType, ipLookupType: IpLookupType.any);

Each discovered service will now have a list of IP addresses attached to it.

Discover all services on local network (regardless of type) #

The current way to do this would be:

  1. Start discovery using special service type _services._dns-sd._udp
  2. Receive list of all service types in network
  3. Do discovery for each service type

Start the discovery like this:

final discovery = await startDiscovery('_services._dns-sd._udp', autoResolve: false);

The autoResolve flag is important because the results are not real services and cannot be resolved. The discovery.services list will then be populated with the answers. The Service instances returned will contain service type info, like so:

{service.type: _tcp.local, service.name: _foo, ...}
{service.type: _tcp.local, service.name: _bar, ...}

The first component of the service type (e.g. _foo) is contained in the service name attribute, the second component of the service type (e.g. _tcp) is contained in the service type attribute.

Even though using a service structure to represent a service type feels like a hack, it seems to be consistent on Android / macOS / iOS platform APIs. Since they are all doing it, the plugin has the same behavior.

Enable debug logging #

In order to help debugging, logging can be enabled for individual topics. For example

enableLogging(LogTopic.calls);

will log all calls to the native side (and their callbacks), which often yields useful information.

For Android #

Add the permission to the manifest:

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
44
likes
0
pub points
91%
popularity

Publisher

verified publisherhaberey.com

A Flutter plugin for network service discovery and registration (aka NSD / DNS-SD / Bonjour / mDNS).

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, nsd_android, nsd_ios, nsd_macos, nsd_platform_interface

More

Packages that depend on nsd