From 68e35729e5a5b7c9757411f2085d1b582fb875f3 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Fri, 25 Aug 2023 14:55:22 +0200 Subject: [PATCH] add scratches of examples --- examples/here_now.rs | 42 +++++++++++++++++++++++++++++++++++++ examples/presence_state.rs | 43 ++++++++++++++++++++++++++++++++++++++ examples/where_now.rs | 0 3 files changed, 85 insertions(+) create mode 100644 examples/here_now.rs create mode 100644 examples/presence_state.rs create mode 100644 examples/where_now.rs diff --git a/examples/here_now.rs b/examples/here_now.rs new file mode 100644 index 00000000..ac9d204d --- /dev/null +++ b/examples/here_now.rs @@ -0,0 +1,42 @@ +use pubnub::{Keyset, PubNubClientBuilder}; +use std::env; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let publish_key = env::var("SDK_PUB_KEY")?; + let subscribe_key = env::var("SDK_SUB_KEY")?; + + let client = PubNubClientBuilder::with_reqwest_transport() + .with_keyset(Keyset { + subscribe_key, + publish_key: Some(publish_key), + secret_key: None, + }) + .with_user_id("user_id") + .build()?; + + println!("running!"); + + let channels_now = client + .here_now() + .channels(["my_channel".into(), "other_channel".into()].to_vec()) + .include_state(true) + .include_user_id(true) + .execute() + .await?; + + println!("All channels data: {:?}", channels_now); + + channels_now.iter().for_each(|channel| { + println!("Channel: {}", channel.name); + println!("Occupancy: {}", channel.occupancy); + println!("Occupants: {:?}", channel.occupants); + + channel + .occupants + .iter() + .for_each(|occupant| println!("Occupant: {:?}", occupant)); + }); + + Ok(()) +} diff --git a/examples/presence_state.rs b/examples/presence_state.rs new file mode 100644 index 00000000..a1521cf0 --- /dev/null +++ b/examples/presence_state.rs @@ -0,0 +1,43 @@ +use pubnub::{Keyset, PubNubClientBuilder}; +use std::env; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let publish_key = env::var("SDK_PUB_KEY")?; + let subscribe_key = env::var("SDK_SUB_KEY")?; + + let client = PubNubClientBuilder::with_reqwest_transport() + .with_keyset(Keyset { + subscribe_key, + publish_key: Some(publish_key), + secret_key: None, + }) + .with_user_id("user_id") + .build()?; + + println!("running!"); + + client + .set_presence_state() + .channels(["my_channel".into(), "other_channel".into()].to_vec()) + .state("{\"What you're doing\": \"Me? Nothing... Just hanging around\"}") + .user_id("user_id") + .execute() + .await?; + + let states = client + .get_presence_state() + .channels(["my_channel".into(), "other_channel".into()].to_vec()) + .user_id("user_id") + .execute() + .await?; + + println!("All channels state: {:?}", states); + + states.iter().for_each(|channel| { + println!("Channel: {}", channel.channel); + println!("State: {:?}", channel.state); + }); + + Ok(()) +} diff --git a/examples/where_now.rs b/examples/where_now.rs new file mode 100644 index 00000000..e69de29b