-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ee): add subscribe request Add the ability to construct and exec subscribe request with the following response parsing. feat: add emit messages / status effects feat: add event listeners Listeners implemented in form of a subscription object which allows polling on updates. test(contract-test): completed contract testing for subscribe Completed set of contract tests for subscription event engine. refactor(clippy): apply clippy suggestions fix: fix formatting warning --------- Co-authored-by: Xavrax <[email protected]>
- Loading branch information
Showing
65 changed files
with
5,966 additions
and
1,163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use futures::StreamExt; | ||
use pubnub::dx::subscribe::{SubscribeStreamEvent, Update}; | ||
use pubnub::{Keyset, PubNubClientBuilder}; | ||
use serde::Deserialize; | ||
use std::env; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Message { | ||
// Allowing dead code because we don't use these fields | ||
// in this example. | ||
#[allow(dead_code)] | ||
url: String, | ||
#[allow(dead_code)] | ||
description: 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 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 subscription = client | ||
.subscribe() | ||
.channels(["my_channel".into(), "other_channel".into()].to_vec()) | ||
.heartbeat(10) | ||
.filter_expression("some_filter") | ||
.execute()?; | ||
|
||
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::MessageAction(action) => { | ||
println!("message action: {:?}", action) | ||
} | ||
Update::File(file) => { | ||
println!("file: {:?}", file) | ||
} | ||
} | ||
} | ||
SubscribeStreamEvent::Status(status) => println!("\nstatus: {:?}", status), | ||
} | ||
})); | ||
|
||
// 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; | ||
|
||
// You can also cancel the subscription at any time. | ||
subscription.unsubscribe().await; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! Deserialization module | ||
//! | ||
//! This module provides a [`Deserialize`] trait for the Pubnub protocol. | ||
//! | ||
//! You can implement this trait for your own types, or use one of the provided | ||
//! features to use a deserialization library. | ||
//! | ||
//! [`Deserialize`]: trait.Deserialize.html | ||
|
||
use crate::core::PubNubError; | ||
|
||
/// Deserialize values | ||
/// | ||
/// This trait provides a [`deserialize`] method for the Pubnub protocol. | ||
/// | ||
/// You can implement this trait for your own types, or use the provided | ||
/// implementations for [`Into<Vec<u8>>`]. | ||
/// | ||
/// [`deserialize`]: #tymethod.deserialize | ||
pub trait Deserialize<'de>: Send + Sync { | ||
/// Type to which binary data should be mapped. | ||
type Type; | ||
|
||
/// Deserialize the value | ||
fn deserialize(bytes: &'de [u8]) -> Result<Self::Type, PubNubError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.