Files
libchat/crates/generic-chat/examples/message-exchange/main.rs
T

74 lines
2.5 KiB
Rust
Raw Normal View History

2026-06-23 12:02:01 -07:00
use components::EphemeralRegistry;
2026-07-03 23:18:10 +02:00
use logos_account::TestLogosAccount;
2026-07-21 09:33:57 -07:00
use logos_generic_chat::{
ChatClientBuilder, DelegateSigner, Event, InProcessDelivery, LogosAuthVerifier, MessageBus,
};
use std::time::Duration;
fn main() {
let bus = MessageBus::default();
let mut reg = EphemeralRegistry::new();
// Mint two accounts, each with a delegate signer, and publish their device
// bundles so a peer can resolve an account address to its device.
let saro_account = TestLogosAccount::new();
let saro_delegate = DelegateSigner::random();
saro_account
.add_delegate_signer(&mut reg, saro_delegate.public_key())
.unwrap();
let raya_account = TestLogosAccount::new();
let raya_delegate = DelegateSigner::random();
raya_account
.add_delegate_signer(&mut reg, raya_delegate.public_key())
.unwrap();
let (mut saro, saro_events) = ChatClientBuilder::new(saro_account.address())
2026-07-21 09:33:57 -07:00
.auth(LogosAuthVerifier::new())
.ident(saro_delegate)
2026-06-23 12:02:01 -07:00
.transport(InProcessDelivery::new(bus.clone()))
.registration(reg.clone())
.build()
.unwrap();
let (mut raya, raya_events) = ChatClientBuilder::new(raya_account.address())
2026-07-21 09:33:57 -07:00
.auth(LogosAuthVerifier::new())
.ident(raya_delegate)
2026-06-23 12:02:01 -07:00
.transport(InProcessDelivery::new(bus))
.registration(reg)
.build()
.unwrap();
// Saro opens a direct conversation with Raya by her account address.
let saro_convo_id = saro.create_direct_conversation(raya.addr()).unwrap();
// Wait for Raya to process the Welcome and subscribe before Saro sends, since
// InProcessDelivery only fans out to current subscribers.
let raya_convo_id = match raya_events.recv_timeout(Duration::from_secs(5)).unwrap() {
Event::ConversationStarted { convo_id, .. } => convo_id,
other => panic!("expected ConversationStarted, got {other:?}"),
};
saro.send_message(&saro_convo_id, b"hello raya").unwrap();
if let Event::MessageReceived { content, .. } =
raya_events.recv_timeout(Duration::from_secs(5)).unwrap()
{
println!(
"Raya received: {:?}",
std::str::from_utf8(&content).unwrap()
);
2026-05-28 23:51:15 +02:00
}
raya.send_message(&raya_convo_id, b"hi saro").unwrap();
if let Event::MessageReceived { content, .. } =
saro_events.recv_timeout(Duration::from_secs(5)).unwrap()
{
println!(
"Saro received: {:?}",
std::str::from_utf8(&content).unwrap()
);
2026-05-28 23:51:15 +02:00
}
println!("Message exchange complete.");
}