Skip to content

Commit

Permalink
chore: showcase publisher_id in topics example
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Dec 18, 2024
1 parent ea32456 commit 577d10b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions example/rust/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ MOMENTO_API_KEY=<YOUR API KEY> cargo run --bin=topics

Example Code: [topics.rs](src/bin/topics.rs)

Note: to see a non-null `publisher_id` on a received [`SubscriptionValue`](https://docs.rs/momento/0.46.1/momento/topics/struct.SubscriptionValue.html), you'll need to set the optional `token_id` argument when programmatically creating a [disposable token](https://docs.momentohq.com/cache/develop/api-reference/auth#generatedisposabletoken).

{{ ossFooter }}
28 changes: 24 additions & 4 deletions example/rust/src/bin/topics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ async fn main() -> MomentoResult<()> {
// call `abort()` on the task handle after messages are published.
let mut subscription1 = topic_client.subscribe("cache", "my-topic").await?;
let subscriber_handle1 = tokio::spawn(async move {
println!("Subscriber should keep receiving until task is aborted");
println!("\n Subscriber [1] should keep receiving until task is aborted");
while let Some(message) = subscription1.next().await {
println!("[1] Received message: {:?}", message);
println!(
"[1] Received message: \n\tKind: {:?} \n\tSequence number: {:?} \n\tSequence page: {:?} \n\tPublisher ID: {:?}",
message.kind,
message.topic_sequence_number,
message.topic_sequence_page,
message.publisher_id
);
}
});

Expand All @@ -38,10 +44,24 @@ async fn main() -> MomentoResult<()> {
// let the task end after receiving 10 messages.
let mut subscription2 = topic_client.subscribe("cache", "my-topic").await?;
tokio::spawn(async move {
println!("Subscriber should receive 10 messages then exit");
println!("\nSubscriber [2] should receive 10 messages then exit");
for _ in 0..10 {
let message = subscription2.next().await;
println!("[2] Received message: {:?}", message);
// println!("[2] Received message: {:?}", message);
match message {
Some(message) => {
println!(
"[2] Received message: \n\tKind: {:?} \n\tSequence number: {:?} \n\tSequence page: {:?} \n\tPublisher ID: {:?}",
message.kind,
message.topic_sequence_number,
message.topic_sequence_page,
message.publisher_id
);
}
None => {
println!("[2] Received None item from subscription");
}
}
}
});

Expand Down

0 comments on commit 577d10b

Please sign in to comment.