diff --git a/src/lib.rs b/src/lib.rs index 0e86640b..d0d494a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,17 +51,20 @@ //! Try the following sample code to get up and running quickly! //! //! ```no_run -//! use pubnub::{Keyset, PubNubClientBuilder}; -//! use pubnub::dx::subscribe::{SubscribeStreamEvent, Update}; +//! use pubnub::subscribe::Subscriber; //! use futures::StreamExt; //! use tokio::time::sleep; //! use std::time::Duration; //! use serde_json; -//! +//! use pubnub::{ +//! dx::subscribe::Update, +//! subscribe::EventSubscriber, +//! Keyset, PubNubClientBuilder, +//! }; //! #[tokio::main] //! async fn main() -> Result<(), Box> { //! use pubnub::subscribe::{EventEmitter, SubscriptionParams}; -//! let publish_key = "my_publish_key"; +//! let publish_key = "my_publish_key"; //! let subscribe_key = "my_subscribe_key"; //! let client = PubNubClientBuilder::with_reqwest_transport() //! .with_keyset(Keyset { @@ -71,15 +74,22 @@ //! }) //! .with_user_id("user_id") //! .build()?; +//! //! println!("PubNub instance created"); -//! +//! //! let subscription = client.subscription(SubscriptionParams { //! channels: Some(&["my_channel"]), //! channel_groups: None, //! options: None //! }); //! -//! println!("Subscribed to channel"); +//! let channel_entity = client.channel("my_channel_2"); +//! let channel_entity_subscription = channel_entity.subscription(None); +//! +//! subscription.subscribe(); +//! channel_entity_subscription.subscribe(); +//! +//! println!("Subscribed to channels"); //! //! // Launch a new task to print out each received message //! tokio::spawn(client.status_stream().for_each(|status| async move { @@ -110,7 +120,21 @@ //! } //! })); //! -//! sleep(Duration::from_secs(1)).await; +//! // Explicitly listen only for real-time `message` updates. +//! tokio::spawn( +//! channel_entity_subscription +//! .messages_stream() +//! .for_each(|message| async move { +//! if let Ok(utf8_message) = String::from_utf8(message.data.clone()) { +//! if let Ok(cleaned) = serde_json::from_str::(&utf8_message) { +//! println!("message: {}", cleaned); +//! } +//! } +//! }), +//! ); +//! +//! sleep(Duration::from_secs(2)).await; +//! //! // Send a message to the channel //! client //! .publish_message("hello world!") @@ -119,7 +143,15 @@ //! .execute() //! .await?; //! -//! sleep(Duration::from_secs(10)).await; +//! // Send a message to another channel +//! client +//! .publish_message("hello world on the other channel!") +//! .channel("my_channel_2") +//! .r#type("text-message") +//! .execute() +//! .await?; +//! +//! sleep(Duration::from_secs(15)).await; //! //! Ok(()) //! }