Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add nats example #239

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ required-features = ["mongo"]
[[example]]
name = "openldap"
required-features = ["openldap"]

[[example]]
name = "nats"
required-features = ["nats"]
43 changes: 43 additions & 0 deletions examples/nats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use async_nats::connect;
use futures::StreamExt;
use testcontainers_modules::{nats::Nats, testcontainers::runners::AsyncRunner};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
// startup the module
let node = Nats::default().start().await?;

// default docker username/password
let default_username = "ruser";
let default_password = "T0pS3cr3t";
let topic = "foo";
let port = node.get_host_port_ipv4(4222).await.unwrap();

// prepare connection string
let connection_url = &format!(
"nats://{}:{}@127.0.0.1:{}",
default_username, default_password, port,
);

let subscriber = connect(connection_url).await.unwrap();
let publisher = connect(connection_url).await.unwrap();

let mut subscription = subscriber.subscribe(topic).await.unwrap();

println!("sending message");
publisher.publish(topic, "Hello".into()).await.unwrap();
publisher.publish(topic, "world".into()).await.unwrap();

let mut messages = Vec::new();
while let Some(message) = subscription.next().await {
messages.push(message.clone());
println!("Received message: {:?}", message);
if messages.len() == 2 {
break;
}
}

assert_eq!(messages.len(), 2);
println!("Received {} messages", messages.len());
Ok(())
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub mod mssql_server;
pub mod mysql;
#[cfg(feature = "nats")]
#[cfg_attr(docsrs, doc(cfg(feature = "nats")))]
/// **Nats** (message orineted middleware) testcontainer
/// **Nats** (message oriented middleware) testcontainer
pub mod nats;
#[cfg(feature = "neo4j")]
#[cfg_attr(docsrs, doc(cfg(feature = "neo4j")))]
Expand Down
Loading