-
Notifications
You must be signed in to change notification settings - Fork 145
Updated "chat" example. Now includes peer discovery [autoconnect] #6
Conversation
@Stebalien Can you please review this PR. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this is adding a significant new feature (rendezvous), I'd make this a new example chat-with-rendezvous
.
chat/README.md
Outdated
@@ -1,49 +1,126 @@ | |||
# p2p chat app with libp2p | |||
# p2p chat app with ```libp2p``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the triple quotes.
chat/README.md
Outdated
|
||
Usage: Run `./chat -sp <SOURCE_PORT>` on host 'B' where <SOURCE_PORT> can be any port number. Now run `./chat -d <MULTIADDR_B>` on node 'A' [`<MULTIADDR_B>` is multiaddress of host 'B' which can be obtained from host 'B' console]. | ||
``` | ||
git clone github.com/libp2p/go-libp2p-examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well just use go get github.com/libp2p/go-libp2p-examples/chat
.
chat/README.md
Outdated
|
||
To build the example, first run `make deps` in the root directory. | ||
Use two different terminal windows to excute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"execute" (or just "run")
chat/README.md
Outdated
|
||
```dht.Provide``` makes this node announce that it can provide a value for the given key. Where a key in this case is ```rendezvousPoint```. Other peers will hit the same key to find other peers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really not the right way to do this. We've been working on an actual rendezvous protocol here:: libp2p/specs#56.
However, given that we already use this hack in pubsub, I won't object too strongly. We should still at least tell users that this is a hack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Stebalien Sure. As go-libp2p-rendezvous is in development I can submit a PR later which includes go-libp2p-rendezvous for peer discovery.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have included a note after explaining provide
and findProvider
.
chat/README.md
Outdated
This function is called on the local peer when a remote peer initiate a connection and starts a stream with the local peer. | ||
```go | ||
// Set a function as stream handler. | ||
host.SetStreamHandler("chat/1.1.0", handleStream) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually start protocols with a leading /
.
chat/README.md
Outdated
host.SetStreamHandler("chat/1.1.0", handleStream) | ||
``` | ||
|
||
```handleStream``` is executed for each new stream incoming to the local peer. ```s new.Stream``` is used to exchange data between local and remote peer. This example uses non blocking functions for reading anf writing from this stream. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is s new.Stream
a typo? (also can use a single backtick).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup 😛 .
also can use a single backtick
next time 👍
chat/README.md
Outdated
|
||
```go | ||
dht := dht.NewDHTClient(ctx, host, datastore.NewMapDatastore()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're deprecating this constructor in favor of dht.New(ctx, host)
(if you don't pass a datastore, it'll construct a map datastore automatically).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modified it. Now using dht.New
chat/README.md
Outdated
|
||
```go | ||
for _, p := range peers { | ||
if err := host.Connect(tctx, p); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, we actually stash the peer addresses in the peerstore. You can skip this step and open a stream directly (it'll automatically connect).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@Stebalien Thanks for the review. |
if this is good to go, i'll merge @upperwal |
Yeah, you can merge it. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bigs wait, hold off on merging. There have been some recent changes upstream in the constructors of go-cid
. As a result this PR is using deprecated functions.
I'll push a commit shortly to update it.
hey @raulk, thanks 😄 |
Resolves: #3. This PR adds peer discovery using dht. Now, all the nodes will auto-connect given a rendezvous string.