Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Updated "chat" example. Now includes peer discovery [autoconnect] #6

Merged
merged 8 commits into from
Sep 24, 2018

Conversation

upperwal
Copy link
Contributor

@upperwal upperwal commented Aug 3, 2018

Resolves: #3. This PR adds peer discovery using dht. Now, all the nodes will auto-connect given a rendezvous string.

@upperwal
Copy link
Contributor Author

@Stebalien Can you please review this PR. Thanks.

Copy link
Member

@Stebalien Stebalien left a 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```
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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.
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Member

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.
Copy link
Member

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).

Copy link
Contributor Author

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())
Copy link
Member

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).

Copy link
Contributor Author

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 {
Copy link
Member

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@upperwal
Copy link
Contributor Author

@Stebalien Thanks for the review.

@bigs
Copy link
Contributor

bigs commented Sep 18, 2018

if this is good to go, i'll merge @upperwal

@bigs bigs added status/in-progress In progress topic/docs Documentation labels Sep 18, 2018
@upperwal
Copy link
Contributor Author

Yeah, you can merge it. Thanks.

@raulk raulk self-requested a review September 18, 2018 18:22
Copy link
Member

@raulk raulk left a 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.

@ghost ghost assigned raulk Sep 18, 2018
@raulk
Copy link
Member

raulk commented Sep 18, 2018

@upperwal this is an awesome example, thanks!

@bigs ready from my end.

@upperwal
Copy link
Contributor Author

hey @raulk, thanks 😄

@bigs bigs merged commit 747b2e2 into libp2p:master Sep 24, 2018
@ghost ghost removed the status/in-progress In progress label Sep 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic/docs Documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants