1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use gun::Gun;
use chia_bls::SecretKey;
use serde_json::json;
use std::sync::Arc;
/// Basic example matching the JavaScript Gun.js quickstart
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate BLS key pair
let secret_key = SecretKey::from_seed(&[0u8; 32]);
let public_key = secret_key.public_key();
// Create a Gun instance
let gun = Arc::new(Gun::new(secret_key, public_key));
// Save data - equivalent to:
// gun.get('mark').put({name: "Mark", email: "mark@gun.eco"})
let mark_chain = gun.get("mark");
mark_chain
.put(json!({
"name": "Mark",
"email": "mark@gun.eco"
}))
.await?;
// Read data - equivalent to:
// gun.get('mark').on((data, key) => { console.log("realtime updates:", data) })
let mark_chain = gun.get("mark");
mark_chain.on(|data, _key| {
println!("realtime updates: {:?}", data);
});
// Live updates - equivalent to:
// setInterval(() => { gun.get('mark').get('live').put(Math.random()) }, 9)
for i in 0..10 {
tokio::time::sleep(tokio::time::Duration::from_millis(9)).await;
gun.get("mark").get("live").put(json!(i as f64)).await?;
}
Ok(())
}