Skip to content

Commit

Permalink
add example and rename function for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Oct 22, 2023
1 parent 56fdb95 commit f8eefed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ async_zmq = { version = "0.4.0", optional = true }
bitcoin = "0.30.0"
futures-util = { version = "0.3.28", optional = true }
zmq = "0.10.0"

# dev dependencies can be used in examples
[dev-dependencies]
futures = "0.3.28"
futures-util = "0.3.28"
17 changes: 17 additions & 0 deletions examples/subscribe_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use bitcoincore_zmq::subscribe_single_async;
use futures_util::StreamExt;

fn main() {
let mut stream = subscribe_single_async("tcp://127.0.0.1:28332").unwrap();

// This is a small example to demonstrate subscribe_single_async, it is okay here to use
// block_on, but not in production environments as this defeats the purpose of async.
futures::executor::block_on(async {
while let Some(msg) = stream.next().await {
match msg {
Ok(msg) => println!("Received message: {msg}"),
Err(err) => println!("Error receiving message: {err}"),
}
}
});
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub use crate::{

#[cfg(feature = "async")]
pub use crate::subscribe::stream::{
subscribe_async, subscribe_multi_async, MessageStream, MultiMessageStream,
subscribe_multi_async, subscribe_single_async, MessageStream, MultiMessageStream,
};
2 changes: 1 addition & 1 deletion src/subscribe/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn subscribe_multi_async(endpoints: &[&str]) -> Result<MultiMessageStream> {
Ok(res)
}

pub fn subscribe_async(endpoint: &str) -> Result<MessageStream> {
pub fn subscribe_single_async(endpoint: &str) -> Result<MessageStream> {
Ok(MessageStream::new(
new_socket_internal(&ZmqContext::new(), endpoint)?.into(),
))
Expand Down

0 comments on commit f8eefed

Please sign in to comment.