Skip to content

Commit 6a16fd3

Browse files
committed
update gossip chat code
1 parent 413dea6 commit 6a16fd3

File tree

1 file changed

+19
-21
lines changed
  • src/app/docs/examples/gossip-chat

1 file changed

+19
-21
lines changed

src/app/docs/examples/gossip-chat/page.mdx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async fn main() -> Result<()> {
5454
// identity for your endpoint. If you want to have
5555
// the same identity each time you open the app,
5656
// you would need to store and load it each time.
57-
let secret_key = SecretKey::generate(rand::rngs::OsRng);
57+
let secret_key = SecretKey::generate(&mut rand::rng());
5858

5959
// Create an endpoint.
6060
// By default we turn on our n0 discovery services.
@@ -109,7 +109,7 @@ async fn main() -> Result<()> {
109109
// and add a clone of the endpoint we have built.
110110
// The gossip protocol will use the endpoint to
111111
// make connections.
112-
let gossip = Gossip::builder().spawn(endpoint.clone()).await?;
112+
let gossip = Gossip::builder().spawn(endpoint.clone());
113113

114114
// The Router is how we manage protocols on top
115115
// of the iroh endpoint. It handles all incoming
@@ -141,7 +141,7 @@ async fn main() -> Result<()> {
141141
let endpoint = Endpoint::bind().await?;
142142

143143
println!("> our endpoint id: {}", endpoint.id());
144-
let gossip = Gossip::builder().spawn(endpoint.clone()).await?;
144+
let gossip = Gossip::builder().spawn(endpoint.clone());
145145

146146
let router = Router::builder(endpoint.clone())
147147
.accept(iroh_gossip::ALPN, gossip.clone())
@@ -155,7 +155,7 @@ async fn main() -> Result<()> {
155155
// Since the `endpoint_ids` list is empty, we will
156156
// subscribe to the topic, but not attempt to
157157
// connect to any other endpoint.
158-
let topic = gossip.subscribe(id, endpoint_ids)?;
158+
let topic = gossip.subscribe(id, endpoint_ids).await?;
159159

160160
// `split` splits the topic into the `GossipSender`
161161
// and `GossipReceiver` portions
@@ -270,7 +270,7 @@ Then implement message reception. We are going to use a separate `subscribe_loop
270270
```rust
271271
// at the top of the file add these imports:
272272
use std::collections::HashMap;
273-
use iroh_gossip::net::{Event, GossipEvent, GossipReceiver};
273+
use iroh_gossip::api::{GossipReceiver,Event};
274274
use futures_lite::StreamExt;
275275

276276
...
@@ -283,7 +283,7 @@ async fn subscribe_loop(mut receiver: GossipReceiver) -> Result<()> {
283283
// iterate over all events
284284
while let Some(event) = receiver.try_next().await? {
285285
// if the Event is a `GossipEvent::Received`, let's deserialize the message:
286-
if let Event::Gossip(GossipEvent::Received(msg)) = event {
286+
if let Event::Received(msg) = event {
287287
// deserialize the message and match on the
288288
// message type:
289289
match Message::from_bytes(&msg.content)?.body {
@@ -300,7 +300,7 @@ async fn subscribe_loop(mut receiver: GossipReceiver) -> Result<()> {
300300
// and print the message
301301
let name = names
302302
.get(&from)
303-
.map_or_else(|| from.fmt_short(), String::to_string);
303+
.map_or_else(|| from.fmt_short().to_string(), String::to_string);
304304
println!("{}: {}", name, text);
305305
}
306306
}
@@ -325,7 +325,8 @@ use futures_lite::StreamExt;
325325
use iroh::protocol::Router;
326326
use iroh::{Endpoint, EndpointId};
327327
use iroh_gossip::{
328-
net::{Event, Gossip, GossipEvent, GossipReceiver},
328+
api::{GossipReceiver,Event},
329+
net::Gossip,
329330
proto::TopicId,
330331
};
331332
use serde::{Deserialize, Serialize};
@@ -335,7 +336,7 @@ async fn main() -> Result<()> {
335336
let endpoint = Endpoint::bind().await?;
336337

337338
println!("> our endpoint id: {}", endpoint.id());
338-
let gossip = Gossip::builder().spawn(endpoint.clone()).await?;
339+
let gossip = Gossip::builder().spawn(endpoint.clone());
339340

340341
let router = Router::builder(endpoint.clone())
341342
.accept(iroh_gossip::ALPN, gossip.clone())
@@ -344,7 +345,7 @@ async fn main() -> Result<()> {
344345
let id = TopicId::from_bytes(rand::random());
345346
let endpoint_ids = vec![];
346347

347-
let (sender, receiver) = gossip.subscribe(id, endpoint_ids)?.split();
348+
let (sender, receiver) = gossip.subscribe(id, endpoint_ids).await?.split();
348349

349350
let message = Message::new(MessageBody::AboutMe {
350351
from: endpoint.id(),
@@ -486,7 +487,7 @@ let ticket = {
486487
// Get our address information, includes our
487488
// `EndpointId`, our `RelayUrl`, and any direct
488489
// addresses.
489-
let me = endpoint.addr().await?;
490+
let me = endpoint.addr();
490491
let endpoints = vec![me];
491492
Ticket { topic: id, endpoints }
492493
};
@@ -535,7 +536,8 @@ use clap::Parser;
535536
use futures_lite::StreamExt;
536537
use iroh::{protocol::Router, Endpoint, EndpointAddr, EndpointId};
537538
use iroh_gossip::{
538-
net::{Event, Gossip, GossipEvent, GossipReceiver},
539+
api::{GossipReceiver,Event},
540+
net::Gossip,
539541
proto::TopicId,
540542
};
541543
use serde::{Deserialize, Serialize};
@@ -591,7 +593,7 @@ async fn main() -> Result<()> {
591593
let endpoint = Endpoint::bind().await?;
592594

593595
println!("> our endpoint id: {}", endpoint.id());
594-
let gossip = Gossip::builder().spawn(endpoint.clone()).await?;
596+
let gossip = Gossip::builder().spawn(endpoint.clone());
595597

596598
let router = Router::builder(endpoint.clone())
597599
.accept(iroh_gossip::ALPN, gossip.clone())
@@ -603,7 +605,7 @@ async fn main() -> Result<()> {
603605
// Get our address information, includes our
604606
// `EndpointId`, our `RelayUrl`, and any direct
605607
// addresses.
606-
let me = endpoint.addr().await?;
608+
let me = endpoint.addr();
607609
let endpoints = vec![me];
608610
Ticket { topic, endpoints }
609611
};
@@ -615,10 +617,6 @@ async fn main() -> Result<()> {
615617
println!("> waiting for endpoints to join us...");
616618
} else {
617619
println!("> trying to connect to {} endpoints...", endpoints.len());
618-
// add the peer addrs from the ticket to our endpoint's addressbook so that they can be dialed
619-
for endpoint_addr in endpoints.into_iter() {
620-
endpoint.add_node_addr(endpoint_addr)?;
621-
}
622620
};
623621
let (sender, receiver) = gossip.subscribe_and_join(topic, endpoint_ids).await?.split();
624622
println!("> connected!");
@@ -696,7 +694,7 @@ async fn subscribe_loop(mut receiver: GossipReceiver) -> Result<()> {
696694
// iterate over all events
697695
while let Some(event) = receiver.try_next().await? {
698696
// if the Event is a `GossipEvent::Received`, let's deserialize the message:
699-
if let Event::Gossip(GossipEvent::Received(msg)) = event {
697+
if let Event::Received(msg) = event {
700698
// deserialize the message and match on the
701699
// message type:
702700
match Message::from_bytes(&msg.content)?.body {
@@ -713,7 +711,7 @@ async fn subscribe_loop(mut receiver: GossipReceiver) -> Result<()> {
713711
// and print the message
714712
let name = names
715713
.get(&from)
716-
.map_or_else(|| from.fmt_short(), String::to_string);
714+
.map_or_else(|| from.fmt_short().to_string(), String::to_string);
717715
println!("{}: {}", name, text);
718716
}
719717
}
@@ -736,7 +734,7 @@ fn input_loop(line_tx: tokio::sync::mpsc::Sender<String>) -> Result<()> {
736734
#[derive(Debug, Serialize, Deserialize)]
737735
struct Ticket {
738736
topic: TopicId,
739-
endpoints: Vec<Addr>,
737+
endpoints: Vec<EndpointAddr>,
740738
}
741739

742740
impl Ticket {

0 commit comments

Comments
 (0)