Skip to content

Commit

Permalink
add scratches of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Aug 28, 2023
1 parent ee7958a commit 68e3572
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/here_now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use pubnub::{Keyset, PubNubClientBuilder};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn snafu::Error>> {
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(())
}
43 changes: 43 additions & 0 deletions examples/presence_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use pubnub::{Keyset, PubNubClientBuilder};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn snafu::Error>> {
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(())
}
Empty file added examples/where_now.rs
Empty file.

0 comments on commit 68e3572

Please sign in to comment.