https://github.com/datl4g/klient2klient
Discover devices in your network and create a connection between them to send data
https://github.com/datl4g/klient2klient
java kotlin library multiplatform p2p
Last synced: 10 months ago
JSON representation
Discover devices in your network and create a connection between them to send data
- Host: GitHub
- URL: https://github.com/datl4g/klient2klient
- Owner: DatL4g
- License: apache-2.0
- Created: 2022-03-01T20:57:38.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-16T18:08:06.000Z (about 1 year ago)
- Last Synced: 2025-03-24T03:34:52.930Z (11 months ago)
- Topics: java, kotlin, library, multiplatform, p2p
- Language: Kotlin
- Homepage:
- Size: 142 KB
- Stars: 21
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Klient2Klient
### Kotlin Multiplatform
This is a Kotlin Multiplatform project, however it is implemented and works ONLY on JVM right now.
This means any JVM project/platform can use this library, so Android and Java Desktop/Server applications can run it.
[](https://github.com/DATL4G/Klient2Klient/issues)
[](https://github.com/DATL4G/Klient2Klient)
[](https://github.com/DATL4G/Klient2Klient/network/members)
[](https://github.com/DATL4G/Klient2Klient/graphs/contributors)
[](https://github.com/DATL4G/Klient2Klient/blob/master/LICENSE)


### Discover and create Peer to Peer connections easily
- Discover hosts in the same network
- Connect and transfer data among clients
### Table of contents
- [Usage](#usage)
- [Discovery](#discovery)
- [Make discoverable](#make-discoverable-can-be-found-by-other-hosts)
- [Start discovery](#start-discovery-find-other-hosts)
- [Get the found hosts](#get-the-found-hosts)
- [Connection](#connection)
- [Start/Stop Receiving](#startstop-receiving)
- [Send/Collect data](#sendcollect-data)
- [Contributing](#contributing)
- [Maintainers](#maintainers)
- [Support the project](#support-the-project)
## Usage
Add the JitPack repository to your build file
```gradle
allprojects {
repositories {
...
maven { url = uri("https://jitpack.io") }
}
}
```
Add the dependency
```gradle
implementation("com.github.DatL4g:Klient2Klient:$latestVersion")
```
## Discovery
The discovery relies on a builder and provides the following options
- ```setDiscoveryTimeout(milliSecondsOrDuration)``` after which time it stops discovering (searching for other hosts)
- ```setDiscoveryTimeoutListener{ }``` called in IO dispatcher but is a suspend function, so you can switch context easily
- ```setDiscoverableTimeout(milliSecondsOrDuration)``` after which time it stops broadcasting to other hosts (cannot be discovered anymore)
- ```setDiscoverableTimeoutListener{ }``` called in IO dispatcher but is a suspend function, so you can switch context easily
- ```setPing(milliSecondsOrDuration)``` at which interval it broadcasts to other hosts
- ```setPuffer(milliSecondsOrDuration)``` tolerance in which time the hosts need to ping again (since it's UDP, packages may be lost or not sent)
- ```setPort(int)``` on which port to broadcast and listen
- ```setHostFilter(regex)``` the discovered hosts need to match this (keep empty (and filterMatch in Host) for any or change the regex to ".*")
- ```setScope(coroutineScope)``` change the scope in which the discovery jobs are running in
- ```setHostIsClient(boolean)``` whether the current host should be discovered ass a client too (default off)
- ```build()``` create an instance of Discovery
The builder can be called using two different methods.
```kotlin
val discover = Discovery.Builder(coroutineScope /* optional */)
.setPort(1337)
.otherBuilderMethods()
.build()
```
```kotlin
val discover = coroutineScope.discovery {
setPort(1337)
otherBuilderMethods()
}
```
### Make discoverable (can be found by other hosts)
Basically the host holds three parameter
- name: the name of the host
- filterMatch: the String to match the hostFilter regex
- optionalInfo: any JsonElement
Call any of the following methods:
```kotlin
discover.makeDiscoverable(Host("name", "filterMatch" /* optional */, jsonElement /* optional */))
```
```kotlin
discover.makeDiscoverable("name", "filterMatch" /* optional */, jsonElement /* optional */)
```
```kotlin
discover.makeDiscoverable("name", "filterMatch" /* optional */, "{jsonString: true}" /* optional */)
```
To stop being discoverable imply call
```kotlin
discover.stopBeingDiscoverable()
```
### Start discovery (find other hosts)
```kotlin
discover.startDiscovery()
//or
discover.startDiscovery(hostIsClient)
```
To stop discovery simply call
```kotlin
discover.stopDiscovery()
```
### Get the found hosts
To get the current list of found hosts synchronously use:
```kotlin
val setOfHosts = discover.peers
```
The host list can be collected to always get the latest items
```kotlin
discover.peersFlow.collect { hosts ->
// got new hosts
}
```
## Connection
The connection relies on a builder as well with these options:
- ```fromDiscovery(discover)``` just to pass the peers
- ```forPeers(setOfHosts)``` pass the peers yourself
- ```forPeer(host)``` only connect to one host
- ```setPort(int)``` the port on which the connection will be established
- ```setScope(coroutineScope)``` change the scope in which the connection jobs are running in
- ```build()``` create an instance of Connection
The builder can be used two different ways again:
```kotlin
val connect = Connection.Builder(coroutineScope /* optional */)
.fromDiscovery(discover)
.setPort(7331)
.build()
```
```kotlin
val connect = coroutineScope.connection {
fromDiscovery(discover)
setPort(7331)
}
```
### Start/Stop Receiving
To start receiving just call, this will allow to get data from another host
```kotlin
connect.startReceiving()
```
Stopping is as easy as starting
```kotlin
connect.stopReceiving()
```
### Send/Collect data
To send data to all connected hosts use
```kotlin
connect.send(bytes)
```
To send data to one host only use
```kotlin
connect.send(bytes, host)
```
Collecting data can be done using Flows again
```kotlin
connect.receiveData.collect { pair: Pair ->
// received data from another host
}
```
## Contributing
When you face any bugs or problems please open an [Issue](https://github.com/DATL4G/Klient2Klient/issues/new/choose).
To add functionality fork the project and create a pull request afterwards. You should know how that works if you are a developer :)
You can add yourself to the list below if you want then.
### Maintainers
| Avatar | Contributor |
|---|:---:|
| [](http://github.com/DatL4g) | [DatLag](http://github.com/DatL4g) |
## Support the project
[](https://github.com/sponsors/DATL4G)
[](https://paypal.me/datlag)
[](https://www.patreon.com/datlag)
Supporting this project helps to keep it up-to-date. You can donate if you want or contribute to the project as well.
This shows that the library is used by people and it's worth to maintain.