callback_session/
callback_ws.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the MIT
3
4use std::{thread, time::Duration};
5
6use reifydb_client::Client;
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9	// Connect to ReifyDB server
10	let client = Client::ws(("127.0.0.1", 8090))?;
11
12	// Create a callback session with authentication
13	let session = client.callback_session(Some("mysecrettoken".to_string()))?;
14
15	// Execute a command to create a table
16	let command_id = session.command(
17		"CREATE NAMESPACE test; CREATE TABLE test.users { id: INT4, name: UTF8 }",
18		None,
19		|result| match result {
20			Ok(data) => println!("Command executed: {} frames returned", data.frames.len()),
21			Err(e) => println!("Command failed: {}", e),
22		},
23	)?;
24	println!("Command sent with ID: {}", command_id);
25
26	// Execute a query
27	let query_id = session.query("MAP { x: 42, y: 'hello' }", None, |result| {
28		match result {
29			Ok(data) => {
30				println!("Query executed: {} frames returned", data.frames.len());
31				// Print first frame if available
32				if let Some(frame) = data.frames.first() {
33					println!("First frame:\n{}", frame);
34				}
35			}
36			Err(e) => println!("Query failed: {}", e),
37		}
38	})?;
39	println!("Query sent with ID: {}", query_id);
40
41	// Wait for callbacks to complete
42	thread::sleep(Duration::from_millis(500));
43
44	Ok(())
45}