Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/appstractive/dns-sd-kt
Kotlin multiplatform implemention of DNS Service Discovery
https://github.com/appstractive/dns-sd-kt
android desktop dns dns-sd ios jvm kotlin macos multiplatform
Last synced: about 2 months ago
JSON representation
Kotlin multiplatform implemention of DNS Service Discovery
- Host: GitHub
- URL: https://github.com/appstractive/dns-sd-kt
- Owner: Appstractive
- License: apache-2.0
- Created: 2024-07-11T12:59:26.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-10-21T10:16:16.000Z (3 months ago)
- Last Synced: 2024-10-21T14:35:53.796Z (3 months ago)
- Topics: android, desktop, dns, dns-sd, ios, jvm, kotlin, macos, multiplatform
- Language: Kotlin
- Homepage:
- Size: 714 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# DNS-SD Kotlin Multiplatform
[![Maven Central](https://img.shields.io/maven-central/v/com.appstractive/dns-sd-kt?label=Maven%20Central)](https://central.sonatype.com/artifact/com.appstractive/dns-sd-kt)
![badge][badge-android]
![badge][badge-ios]
![badge][badge-macos]
![badge][badge-tvos]
![badge][badge-jvm]This library implements [Multicast DNS][mdns] and [DNS-Based Service Discovery][dnssd] to provide
zero-configuration operations for Kotlin Multiplatform. It lets you announce and find services in a
.local domain.The platform implementations are based on:
- Android: [Network Service Discovery (NSD)](https://developer.android.com/develop/connectivity/wifi/use-nsd)
- Apple: [Bonjour](https://developer.apple.com/documentation/foundation/bonjour)
- JVM: [JmDNS](https://github.com/jmdns/jmdns)## Usage
### Installation
Gradle:
```
implementation("com.appstractive:dns-sd-kt:1.0.1")
```### Permissions
#### Android
Add the following permissions to your manifest:
```Xml
```
#### Apple
Add the following permissions to your Info.plist (replace service type with your own):
```Xml
NSLocalNetworkUsageDescription
Required to discover local network devices
NSBonjourServices_http._tcp
```
### Publish a service
Manually:
```kotlin
val service = createNetService(
type = "_myservice._tcp",
name = "MyService",
) {
port = 8080
addresses = null // platforms default addresses
txt =
mapOf(
"key1" to "value1",
"key2" to "value2",
)
}// start publication to network
service.register()if(service.registered) {
// stop publication to network
service.unregister()
}
```Scope based:
```kotlin
val scope = CoroutineScope(Dispatchers.Main) // or lifecycleScope/viewModelScopescope.launch {
publishService(
type = "_myservice._tcp",
name = "MyService",
) {
port = 8080
addresses = null // platforms default addresses
txt =
mapOf(
"key1" to "value1",
"key2" to "value2",
)
}
}// when the scope is cancelled, the service automatically unregisters itself
scope.cancel()```
### Discover services
```kotlin
val services: Map = mutableMapOf()discoverServices("_myservice._tcp").collect {
when (it) {
is DiscoveryEvent.Discovered -> {
scannedServices[it.service.key] = it.service
// optionally resolve ip addresses of the service
it.resolve()
}
is DiscoveryEvent.Removed -> {
scannedServices.remove(it.service.key)
}
is DiscoveryEvent.Resolved -> {
scannedServices[it.service.key] = it.service
}
}
}
```## Sample App
This repository contains a sample compose multiplatform application to demonstrate the usage on the
platforms Android, iOS and Desktop JVM.| Android | iOS | Desktop JVM |
|-------------------------------------------------------------------------|-----------------------------------------------------------------|-----------------------------------------------------------------------------|
| | | |### Run
#### Android
```
./gradlew installDebug
```#### iOS
Open sample/iosApp/iosApp.xcworkspace in XCode build and run
#### JVM
```
./gradlew ":sample:composeApp:run"
```[mdns]: https://tools.ietf.org/html/rfc6762
[dnssd]: https://tools.ietf.org/html/rfc6763
[badge-android]: http://img.shields.io/badge/platform-android-6EDB8D.svg?style=flat
[badge-ios]: http://img.shields.io/badge/platform-ios-CDCDCD.svg?style=flat
[badge-macos]: http://img.shields.io/badge/platform-macos-111111.svg?style=flat
[badge-tvos]: http://img.shields.io/badge/platform-tvos-808080.svg?style=flat
[badge-jvm]: http://img.shields.io/badge/platform-jvm-CDCDCD.svg?style=flat