-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,9 +90,115 @@ See for an in-depth description of libp2p, please see: https://libp2p.io/ | |
|
||
## Getting started | ||
|
||
First add the proper dependencies to your project: | ||
|
||
Kotlin DSL: | ||
|
||
```kotlin | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("org.erwinkok.result:libp2p-xxx:$latest") | ||
} | ||
``` | ||
|
||
`libp2p-core` is mandatory, other dependencies are optional depending on your needs. For example, if you need tcp | ||
transport, include `libp2p-transport-tcp`, or if you want the mplex muxer include `libp2p-muxer-plex`. | ||
|
||
In your code, first create a host: | ||
|
||
```kotlin | ||
val hostBuilder = host { | ||
identity(localIdentity) | ||
muxers { | ||
mplex() | ||
} | ||
securityTransport { | ||
noise() | ||
} | ||
transports { | ||
tcp() | ||
} | ||
peerstore { | ||
gcInterval = 1.hours | ||
keyStore { | ||
password = "APasswordThatIsAtLeast20CharactersLong" | ||
dek { | ||
salt = "W/SC6fnZfBIWdeAD3l+ClLpQtfICEtn+KYTUhfKq6d7l" | ||
} | ||
} | ||
} | ||
swarm { | ||
dialTimeout = 10.minutes | ||
listenAddresses { | ||
multiAddress("/ip4/0.0.0.0/tcp/10333") | ||
} | ||
} | ||
datastore(datastore) | ||
} | ||
|
||
val host = hostBuilder.build(scope) | ||
.getOrElse { | ||
logger.error { "The following errors occurred while creating the host: ${errorMessage(it)}" } | ||
return@runBlocking | ||
} | ||
``` | ||
|
||
The layout is hopefully clear: for example, the code above will use `tcp` as a transport, `noise` for security and | ||
`mplex` as a muxer. | ||
|
||
Then you can add a handler: | ||
|
||
```kotlin | ||
host.setStreamHandler(ProtocolId.of("/chat/1.0.0")) { | ||
chatHandler(it) | ||
} | ||
``` | ||
This means that if a peer connects and requests the `/chat/1.0.0` protocol, the corresponding handler will be called. | ||
|
||
To call a peer and open a new stream, use the following code: | ||
|
||
```kotlin | ||
val stream = host.newStream(aPeerId, ProtocolId.of("/chat/1.0.0")) | ||
.getOrElse { | ||
logger.error { "Could not open chat stream with peer: ${errorMessage(it)}" } | ||
return@runBlocking | ||
} | ||
chatHandler(stream) | ||
``` | ||
|
||
This tries to connect to peer `aPeerId` and tries to open a new stream for protocol `/chat/1.0.0`. If it fails, it | ||
returns if it succeeds it progresses to the chatHandler. | ||
|
||
See also the example application in `app`. | ||
|
||
To use this sample app, start the application. It will create a new random LocalIdentity (key-pair) and logs the adres | ||
on which it listens on the output: | ||
|
||
```shell | ||
[main] o.e.l.a.ApplicationKt$main$1: Local addresses the Host listens on: /ip4/0.0.0.0/tcp/10333/p2p/12D3KooWDfaEJxpmjbFLb9wUakCd6Lo6LRntaV3drb4EaYZRtYuY | ||
``` | ||
|
||
In the libp2p-Go repository you can find `chat` in the examples directory. Then you can connect to the running instance | ||
by using: | ||
|
||
```shell | ||
./chat -d /ip4/0.0.0.0/tcp/10333/p2p/12D3KooWDfaEJxpmjbFLb9wUakCd6Lo6LRntaV3drb4EaYZRtYuY | ||
``` | ||
|
||
On both sides it should mention that a connection is established. | ||
|
||
## Contact | ||
|
||
If you want to contact me, please write an e-mail to: [[email protected]](mailto:[email protected]) | ||
If you want to contact me, please write an e-mail to: [[email protected]](mailto:[email protected]) | ||
|
||
## Acknowledgements | ||
|
||
This work is largely based on the awesome libp2p-go implementation. This work would not have been possible without their | ||
effort. Please consider giving kudos to the libp2p-go authors. | ||
(See also [`ACKNOWLEDGEMENTS`](ACKNOWLEDGEMENTS.md)) | ||
|
||
## License | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters