Skip to content

Commit

Permalink
sdk: add redb example
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Jan 24, 2025
1 parent 854a158 commit 404e2dd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/nostr-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ nostr-indexeddb = { workspace = true, optional = true }

[dev-dependencies]
nostr-connect.workspace = true
nostr-redb.workspace = true
tokio = { workspace = true, features = ["macros"] }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

Expand Down Expand Up @@ -88,6 +89,9 @@ required-features = ["all-nips"]
name = "nostrdb"
required-features = ["ndb"]

[[example]]
name = "redb"

[[example]]
name = "stream-events"

Expand Down
63 changes: 63 additions & 0 deletions crates/nostr-sdk/examples/redb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use nostr_redb::NostrRedb;
use nostr_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let keys = Keys::parse("nsec1ufnus6pju578ste3v90xd5m2decpuzpql2295m3sknqcjzyys9ls0qlc85")?;

let database = NostrRedb::persistent("./db/nostr-redb")?;
let client: Client = ClientBuilder::default()
.signer(keys.clone())
.database(database)
.build();

client.add_relay("wss://relay.damus.io").await?;
client.add_relay("wss://nostr.wine").await?;
client.add_relay("wss://nostr.oxtr.dev").await?;

client.connect().await;

// Publish a text note
let builder = EventBuilder::text_note("Hello world");
client.send_event_builder(builder).await?;

// Negentropy sync
let filter = Filter::new().author(keys.public_key());
let (tx, mut rx) = SyncProgress::channel();
let opts = SyncOptions::default().progress(tx);

tokio::spawn(async move {
while rx.changed().await.is_ok() {
let progress = *rx.borrow_and_update();
if progress.total > 0 {
println!("{:.2}%", progress.percentage() * 100.0);
}
}
});
let output = client.sync(filter, &opts).await?;

println!("Local: {}", output.local.len());
println!("Remote: {}", output.remote.len());
println!("Sent: {}", output.sent.len());
println!("Received: {}", output.received.len());
println!("Failures:");
for (url, map) in output.send_failures.iter() {
println!("* '{url}':");
for (id, e) in map.iter() {
println!(" - {id}: {e}");
}
}

// Query events from database
let filter = Filter::new().author(keys.public_key()).limit(10);
let events = client.database().query(vec![filter]).await?;
println!("Events: {events:?}");

Ok(())
}

0 comments on commit 404e2dd

Please sign in to comment.