Skip to content

Commit

Permalink
feat(providers): spawn task for listening to stream in ws
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Mar 19, 2024
1 parent 8e0af98 commit 9ba010d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
8 changes: 8 additions & 0 deletions examples/providers/examples/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ async fn main() -> Result<()> {
println!("{:?}", block.header.number);
}

let handle = tokio::spawn(async move {
while let Some(block) = stream.next().await {
println!("{:?}", block.header.number);
}
});

handle.await?;

Ok(())
}
23 changes: 16 additions & 7 deletions examples/providers/examples/ws_with_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ async fn main() -> Result<()> {

println!("Awaiting blocks...");

while let Some(block) = stream_basic.next().await {
println!("From basic {:?}", block.header.number);
}

while let Some(block) = stream_bearer.next().await {
println!("From bearer {:?}", block.header.number);
}
// Spawning the block processing for basic auth as a new task
let basic_handle = tokio::spawn(async move {
while let Some(block) = stream_basic.next().await {
println!("From basic {:?}", block.header.number);
}
});

// Similarly for bearer auth
let bearer_handle = tokio::spawn(async move {
while let Some(block) = stream_bearer.next().await {
println!("From bearer {:?}", block.header.number);
}
});

// Wait for both tasks to complete
let _ = tokio::try_join!(basic_handle, bearer_handle)?;

Ok(())
}

0 comments on commit 9ba010d

Please sign in to comment.