Skip to content

Commit

Permalink
changed the example in lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
techwritermat committed Feb 5, 2024
1 parent a0ee380 commit b27ed67
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
//! 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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<String>(&utf8_message) {
//! println!("message: {}", cleaned);
//! }
//! }
//! }),
//! );
//!
//! sleep(Duration::from_secs(2)).await;
//!
//! // Send a message to the channel
//! client
//! .publish_message("hello world!")
Expand All @@ -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(())
//! }
Expand Down

0 comments on commit b27ed67

Please sign in to comment.