Skip to content

Commit e7ac572

Browse files
committed
sdk: add redb example
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent f37837a commit e7ac572

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nostr-sdk/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ nostr-indexeddb = { workspace = true, optional = true }
5353

5454
[dev-dependencies]
5555
nostr-connect.workspace = true
56+
nostr-redb.workspace = true
5657
tokio = { workspace = true, features = ["macros"] }
5758
tracing-subscriber = { workspace = true, features = ["env-filter"] }
5859

@@ -88,6 +89,9 @@ required-features = ["all-nips"]
8889
name = "nostrdb"
8990
required-features = ["ndb"]
9091

92+
[[example]]
93+
name = "redb"
94+
9195
[[example]]
9296
name = "stream-events"
9397

crates/nostr-sdk/examples/redb.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2022-2023 Yuki Kishimoto
2+
// Copyright (c) 2023-2024 Rust Nostr Developers
3+
// Distributed under the MIT software license
4+
5+
use nostr_redb::NostrRedb;
6+
use nostr_sdk::prelude::*;
7+
8+
#[tokio::main]
9+
async fn main() -> Result<()> {
10+
tracing_subscriber::fmt::init();
11+
12+
let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;
13+
14+
let database = NostrRedb::persistent("./db/nostr-redb")?;
15+
let client: Client = ClientBuilder::default()
16+
.signer(keys.clone())
17+
.database(database)
18+
.build();
19+
20+
client.add_relay("wss://relay.damus.io").await?;
21+
client.add_relay("wss://nostr.wine").await?;
22+
client.add_relay("wss://nostr.oxtr.dev").await?;
23+
24+
client.connect().await;
25+
26+
// Publish a text note
27+
let builder = EventBuilder::text_note("Hello world");
28+
client.send_event_builder(builder).await?;
29+
30+
// Negentropy sync
31+
let filter = Filter::new().author(keys.public_key());
32+
let (tx, mut rx) = SyncProgress::channel();
33+
let opts = SyncOptions::default().progress(tx);
34+
35+
tokio::spawn(async move {
36+
while rx.changed().await.is_ok() {
37+
let progress = *rx.borrow_and_update();
38+
if progress.total > 0 {
39+
println!("{:.2}%", progress.percentage() * 100.0);
40+
}
41+
}
42+
});
43+
let output = client.sync(filter, &opts).await?;
44+
45+
println!("Local: {}", output.local.len());
46+
println!("Remote: {}", output.remote.len());
47+
println!("Sent: {}", output.sent.len());
48+
println!("Received: {}", output.received.len());
49+
println!("Failures:");
50+
for (url, map) in output.send_failures.iter() {
51+
println!("* '{url}':");
52+
for (id, e) in map.iter() {
53+
println!(" - {id}: {e}");
54+
}
55+
}
56+
57+
// Query events from database
58+
let filter = Filter::new().author(keys.public_key()).limit(10);
59+
let events = client.database().query(vec![filter]).await?;
60+
println!("Events: {events:?}");
61+
62+
Ok(())
63+
}

0 commit comments

Comments
 (0)