Depending on your project targets, you need at least :
NWBrowser
.This plugin cannot be tested on an Android emulator (well it can, but the only services that you are able to discover are the ones broadcasted by your emulator).
In your Flutter project directory, run the following commands :
flutter pub add bonsoir
flutter pub get
If you want to use this plugin on iOS, you must update your deployment target to at least 13.0.
At the top of ios/Podfile
, add the following :
platform :ios, '13.0'
Also, open your iOS project in Xcode and select Runner, Targets -> Runner and then the "General" tab. Under the "Minimum Deployments" section, update the iOS version to 13.0 or higher.
If you're building your app for iOS 14 or higher, you have to edit your Info.plist
file. Just add
the following lines :
<key>NSLocalNetworkUsageDescription</key>
<string>Describe here why you want to use Bonsoir.</string>
<key>NSBonjourServices</key>
<array>
<string>_first-service._tcp</string>
<string>_second-service._tcp</string>
<string>_third-service._tcp</string>
<!-- Add more here -->
</array>
Don't forget to edit them according to your needs.
If you want to use this plugin on macOS, you must update your deployment target to at least 10.15.
At the top of macos/Podfile
, add the following :
platform :ios, '10.15'
Also, open your macOS project in Xcode and select Runner, Targets -> Runner and then the "General" tab. Under the "Minimum Deployments" section, update the macOS version to 10.15 or higher.
If you don't have Avahi installed on your system, just install it using :
sudo apt install -y avahi-daemon avahi-discover avahi-utils libnss-mdns mdns-scan
Bonsoir has been made to be as easy to use as possible.
Here is how you can broadcast your service using Bonsoir :
// Let's create our service !
BonsoirService service = BonsoirService(
name: 'My wonderful service', // Put your service name here.
type: '_wonderful-service._tcp', // Put your service type here. Syntax : _ServiceType._TransportProtocolName. (see http://wiki.ros.org/zeroconf/Tutorials/Understanding%20Zeroconf%20Service%20Types).
port: 3030, // Put your service port here.
);
// And now we can broadcast it :
BonsoirBroadcast broadcast = BonsoirBroadcast(service: service);
await broadcast.ready;
await broadcast.start();
// ...
// Then if you want to stop the broadcast :
await broadcast.stop();
Here is how you can search for a broadcasted service :
// This is the type of service we're looking for :
String type = '_wonderful-service._tcp';
// Once defined, we can start the discovery :
BonsoirDiscovery discovery = BonsoirDiscovery(type: type);
await discovery.ready;
// If you want to listen to the discovery :
discovery.eventStream!.listen((event) { // `eventStream` is not null as the discovery instance is "ready" !
if (event.type == BonsoirDiscoveryEventType.discoveryServiceFound) {
print('Service found : ${event.service.toJson()}')
event.service!.resolve(discovery.serviceResolver); // Should be called when the user wants to connect to this service.
} else if (event.type == BonsoirDiscoveryEventType.discoveryServiceResolved) {
print('Service resolved : ${event.service.toJson()}')
} else if (event.type == BonsoirDiscoveryEventType.discoveryServiceLost) {
print('Service lost : ${event.service.toJson()}')
}
});
// Start the discovery **after** listening to discovery events :
await discovery.start();
// Then if you want to stop the discovery :
await discovery.stop();
Note. If you're transitioning from multicast_dns
, note that types don't end with .local
.
If you want a full
example, don't hesitate to check
this one on Github.
To run it :
melos bs
.packages/bonsoir/example
directory, and run flutter run
.You have a lot of options to contribute to this project ! You can :
This plugin was initially created to use in my game, Mobile Werewolf. It is an unofficial mobile version of the famous board game Mafia / Werewolves. In this game, players can play against each others via Wi-Fi, so Bonsoir allows them to easily broadcast, discover and join local network parties.
The hand icon has been created by Vitaly Gorbachev.