-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpresence_example.rs
More file actions
64 lines (57 loc) · 2.08 KB
/
presence_example.rs
File metadata and controls
64 lines (57 loc) · 2.08 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use core::time::Duration;
use examples::get_supabase_credentials;
use rp_supabase_auth::jwt_stream::SupabaseAuthConfig;
use rp_supabase_auth::types::LoginCredentials;
use rp_supabase_realtime::futures::StreamExt as _;
use rp_supabase_realtime::realtime::{self, Presence};
use serde::{Deserialize, Serialize};
use tracing_subscriber::EnvFilter;
#[derive(Debug, Deserialize, Serialize)]
struct MessageToTrack {
example_field_b: String,
field_a: String,
random_number: u64,
}
#[tokio::main]
async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.from_env()?
.add_directive("rp_supabase_auth=info".to_owned().parse()?)
.add_directive("rp_supabase_realtime=info".to_owned().parse()?)
.add_directive("examples=info".to_owned().parse()?)
.add_directive("presence_example=info".to_owned().parse()?),
)
.init();
color_eyre::install()?;
let credentials = get_supabase_credentials()?;
let config = SupabaseAuthConfig {
api_key: credentials.anon_key,
max_reconnect_attempts: 5,
reconnect_interval: Duration::from_secs(3),
url: credentials.supabase_api_url,
};
let login_credentials = LoginCredentials::builder()
.email(credentials.email)
.password(credentials.password)
.build();
let (mut realtime, mut client) =
realtime::RealtimeConnection::<Presence>::channel_presence(config, "af")
.connect_with_state_tracking::<simd_json::OwnedValue>(login_credentials)
.await?;
client.join(None).await?;
client
.track(&MessageToTrack {
field_a: "todo!()".to_owned(),
example_field_b: "data b inner".to_owned(),
random_number: 123,
})
.await?;
tracing::info!("pooling realtime connection");
while let Some(msg) = realtime.next().await {
tracing::info!(?msg, "message");
}
tracing::error!("realtime connection exited");
eyre::bail!("unexpected realtime exit")
}