Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event listener interface and presence changes #183

Merged
merged 11 commits into from
Jan 26, 2024
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ std = ["derive_builder/std", "log/std", "uuid/std", "base64/std", "spin/std", "s
extra_platforms = ["spin/portable_atomic", "dep:portable-atomic"]

# [Internal features] (not intended for use outside of the library)
contract_test = ["parse_token", "publish", "access", "crypto"]
contract_test = ["parse_token", "publish", "access", "crypto", "std", "subscribe", "presence", "tokio"]
full_no_std = ["serde", "reqwest", "crypto", "parse_token", "blocking", "publish", "access", "subscribe", "tokio", "presence"]
full_no_std_platform_independent = ["serde", "crypto", "parse_token", "blocking", "publish", "access", "subscribe", "presence"]
pubnub_only = ["crypto", "parse_token", "blocking", "publish", "access", "subscribe", "presence"]
Expand Down Expand Up @@ -106,7 +106,7 @@ getrandom = { version = "0.2", optional = true }
# parse_token
ciborium = { version = "0.2.1", default-features = false, optional = true }

# subscribe
# subscribe, presence
futures = { version = "0.3.28", default-features = false, optional = true }
tokio = { version = "1", optional = true, features = ["rt-multi-thread", "macros", "time"] }
async-channel = { version = "1.8", optional = true }
Expand All @@ -122,7 +122,7 @@ async-trait = "0.1"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
wiremock = "0.5"
env_logger = "0.10"
cucumber = { version = "0.20.0", features = ["output-junit"] }
cucumber = { version = "0.20.2", features = ["output-junit"] }
reqwest = { version = "0.11", features = ["json"] }
test-case = "3.0"
hashbrown = { version = "0.14.0", features = ["serde"] }
Expand Down Expand Up @@ -165,7 +165,7 @@ required-features = ["default"]

[[example]]
name = "subscribe"
required-features = ["default", "subscribe"]
required-features = ["default", "subscribe", "presence"]

[[example]]
name = "subscribe_raw"
Expand Down
26 changes: 14 additions & 12 deletions examples/no_std/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ getrandom::register_custom_getrandom!(custom_random);
fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {
// We're using `42` as a random number, because it's the answer
// to the Ultimate Question of Life, the Universe, and Everything.
// In your program, you should use proper random number generator that is supported by your target.
// In your program, you should use proper random number generator that is
// supported by your target.
for i in buf.iter_mut() {
*i = 42;
}
Expand All @@ -48,7 +49,8 @@ fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> {

// Many targets have very specific requirements for networking, so it's hard to
// provide a generic implementation.
// Depending on the target, you will probably need to implement `Transport` trait.
// Depending on the target, you will probably need to implement `Transport`
// trait.
struct MyTransport;

impl Transport for MyTransport {
Expand All @@ -64,8 +66,8 @@ impl Transport for MyTransport {
// As our target does not have `std` library, we need to provide custom
// implementation of `GlobalAlloc` trait.
//
// In your program, you should use proper allocator that is supported by your target.
// Here you have dummy implementation that does nothing.
// In your program, you should use proper allocator that is supported by your
// target. Here you have dummy implementation that does nothing.
#[derive(Default)]
pub struct Allocator;

Expand All @@ -82,23 +84,23 @@ static GLOBAL_ALLOCATOR: Allocator = Allocator;
// As our target does not have `std` library, we need to provide custom
// implementation of `panic_handler`.
//
// In your program, you should use proper panic handler that is supported by your target.
// Here you have dummy implementation that does nothing.
// In your program, you should use proper panic handler that is supported by
// your target. Here you have dummy implementation that does nothing.
#[panic_handler]
fn panicking(_: &PanicInfo) -> ! {
loop {}
}

// As we're using `no_main` attribute, we need to define `main` function manually.
// For this example we're using `extern "C"` ABI to make it work.
// As we're using `no_main` attribute, we need to define `main` function
// manually. For this example we're using `extern "C"` ABI to make it work.
#[no_mangle]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> usize {
publish_example().map(|_| 0).unwrap()
}

// In standard subscribe examples we use `println` macro to print the result of the operation
// and it shows the idea of the example. `no_std` does not support `println` macro,
// so we're using `do_a_thing` function instead.
// In standard subscribe examples we use `println` macro to print the result of
// the operation and it shows the idea of the example. `no_std` does not support
// `println` macro, so we're using `do_a_thing` function instead.
fn do_a_thing<T>(_: T) {}

// As `no_std` does not support `Error` trait, we use `PubNubError` instead.
Expand Down Expand Up @@ -133,7 +135,7 @@ fn publish_example() -> Result<(), PubNubError> {
match update? {
Update::Message(message) | Update::Signal(message) => do_a_thing(message),
Update::Presence(presence) => do_a_thing(presence),
Update::Object(object) => do_a_thing(object),
Update::AppContext(object) => do_a_thing(object),
Update::MessageAction(action) => do_a_thing(action),
Update::File(file) => do_a_thing(file),
};
Expand Down
41 changes: 36 additions & 5 deletions examples/presence_state.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use std::collections::HashMap;

use pubnub::{Keyset, PubNubClientBuilder};
use serde::Serialize;
parfeon marked this conversation as resolved.
Show resolved Hide resolved
use std::env;

#[derive(Debug, Serialize)]
#[derive(Debug, serde::Serialize)]
struct State {
is_doing: String,
flag: bool,
}
#[derive(Debug, serde::Serialize)]
struct State2 {
is_doing: String,
business: String,
}

#[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 publish_key = env::var("SDK_PUB_KEY")?;
// let subscribe_key = env::var("SDK_SUB_KEY")?;
let publish_key = "demo";
let subscribe_key = "demo";

let client = PubNubClientBuilder::with_reqwest_transport()
.with_keyset(Keyset {
Expand All @@ -23,9 +31,32 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {

println!("running!");

client
.set_presence_state_with_heartbeat(HashMap::from([
(
"my_channel".to_string(),
State {
is_doing: "Something".to_string(),
flag: true,
},
),
(
"other_channel".to_string(),
State {
is_doing: "Oh no".to_string(),
flag: false,
},
),
]))
.channels(["my_channel".into(), "other_channel".into()].to_vec())
.user_id("user_id")
.execute()
.await?;

client
.set_presence_state(State {
is_doing: "Nothing... Just hanging around...".into(),
flag: false,
})
.channels(["my_channel".into(), "other_channel".into()].to_vec())
.user_id("user_id")
Expand Down
135 changes: 103 additions & 32 deletions examples/subscribe.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use std::collections::HashMap;

use futures::StreamExt;
use pubnub::dx::subscribe::{SubscribeStreamEvent, Update};
use pubnub::{Keyset, PubNubClientBuilder};
use serde::Deserialize;
use std::env;

use pubnub::subscribe::{SubscriptionOptions, SubscriptionParams};
use pubnub::{
dx::subscribe::Update,
subscribe::{EventEmitter, EventSubscriber},
Keyset, PubNubClientBuilder,
};

#[derive(Debug, Deserialize)]
struct Message {
// Allowing dead code because we don't use these fields
Expand All @@ -26,59 +33,123 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
secret_key: None,
})
.with_user_id("user_id")
.with_filter_expression("some_filter")
.with_heartbeat_value(100)
.with_heartbeat_interval(5)
.build()?;

println!("running!");

let subscription = client
.subscribe()
client
.set_presence_state(HashMap::<String, String>::from([
(
"is_doing".to_string(),
"Nothing... Just hanging around...".to_string(),
),
("flag".to_string(), "false".to_string()),
]))
.channels(["my_channel".into(), "other_channel".into()].to_vec())
.heartbeat(10)
.filter_expression("some_filter")
.execute()?;
.user_id("user_id")
.execute()
.await?;

tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;

let subscription = client.subscription(SubscriptionParams {
channels: Some(&["my_channel", "other_channel"]),
channel_groups: None,
options: Some(vec![SubscriptionOptions::ReceivePresenceEvents]),
});
subscription.subscribe(None);
let subscription_clone = subscription.clone_empty();

// Attach connection status to the PubNub client instance.
tokio::spawn(
client
.status_stream()
.for_each(|status| async move { println!("\nstatus: {:?}", status) }),
);

tokio::spawn(subscription.stream().for_each(|event| async move {
match event {
SubscribeStreamEvent::Update(update) => {
println!("\nupdate: {:?}", update);
match update {
Update::Message(message) | Update::Signal(message) => {
// Deserialize the message payload as you wish
match serde_json::from_slice::<Message>(&message.data) {
Ok(message) => println!("defined message: {:?}", message),
Err(_) => {
println!("other message: {:?}", String::from_utf8(message.data))
}
}
}
Update::Presence(presence) => {
println!("presence: {:?}", presence)
}
Update::Object(object) => {
println!("object: {:?}", object)
Update::Message(message) | Update::Signal(message) => {
// Deserialize the message payload as you wish
match serde_json::from_slice::<Message>(&message.data) {
Ok(message) => println!("(a) defined message: {:?}", message),
Err(_) => {
println!("(a) other message: {:?}", String::from_utf8(message.data))
}
Update::MessageAction(action) => {
println!("message action: {:?}", action)
}
Update::File(file) => {
println!("file: {:?}", file)
}
}
Update::Presence(presence) => {
println!("(a) presence: {:?}", presence)
}
Update::AppContext(object) => {
println!("(a) object: {:?}", object)
}
Update::MessageAction(action) => {
println!("(a) message action: {:?}", action)
}
Update::File(file) => {
println!("(a) file: {:?}", file)
}
}
}));

tokio::spawn(subscription_clone.stream().for_each(|event| async move {
match event {
Update::Message(message) | Update::Signal(message) => {
// Deserialize the message payload as you wish
match serde_json::from_slice::<Message>(&message.data) {
Ok(message) => println!("(b) defined message: {:?}", message),
Err(_) => {
println!("(b) other message: {:?}", String::from_utf8(message.data))
}
}
}
SubscribeStreamEvent::Status(status) => println!("\nstatus: {:?}", status),
Update::Presence(presence) => {
println!("(b) presence: {:?}", presence)
}
Update::AppContext(object) => {
println!("(b) object: {:?}", object)
}
Update::MessageAction(action) => {
println!("(b) message action: {:?}", action)
}
Update::File(file) => {
println!("(b) file: {:?}", file)
}
}
}));

// Sleep for a minute. Now you can send messages to the channels
// "my_channel" and "other_channel" and see them printed in the console.
// You can use the publish example or [PubNub console](https://www.pubnub.com/docs/console/)
// to send messages.
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(15)).await;

// You can also cancel the subscription at any time.
subscription.unsubscribe().await;
// subscription.unsubscribe();

println!("\nDisconnect from the real-time data stream");
client.disconnect();

tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;

println!("\nReconnect to the real-time data stream");
client.reconnect(None);

// Let event engine process unsubscribe request
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;

// If Subscription or Subscription will go out of scope they will unsubscribe.
// drop(subscription);
// drop(subscription_clone);

println!(
"\nUnsubscribe from all data streams. To restore requires `subscription.subscribe(None)` call." );
// Clean up before complete work with PubNub client instance.
client.unsubscribe_all();
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/subscribe_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
Update::Presence(presence) => {
println!("presence: {:?}", presence)
}
Update::Object(object) => {
Update::AppContext(object) => {
println!("object: {:?}", object)
}
Update::MessageAction(action) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/subscribe_raw_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> Result<(), Box<dyn snafu::Error>> {
Update::Presence(presence) => {
println!("presence: {:?}", presence)
}
Update::Object(object) => {
Update::AppContext(object) => {
println!("object: {:?}", object)
}
Update::MessageAction(action) => {
Expand Down
Loading
Loading