@@ -35,11 +35,11 @@ Add `pubnub` to your Rust project in the `Cargo.toml` file:
3535``` toml
3636# default features
3737[dependencies ]
38- pubnub = " 0.2.1 "
38+ pubnub = " 0.3.0 "
3939
4040# all features
4141[dependencies ]
42- pubnub = { version = " 0.2.1 " , features = [" full" ] }
42+ pubnub = { version = " 0.3.0 " , features = [" full" ] }
4343```
4444
4545### Example
@@ -48,28 +48,75 @@ Try the following sample code to get up and running quickly!
4848
4949``` rust
5050use pubnub :: {Keyset , PubNubClientBuilder };
51+ use pubnub :: dx :: subscribe :: {SubscribeStreamEvent , Update };
52+ use futures :: StreamExt ;
53+ use tokio :: time :: sleep;
54+ use std :: time :: Duration ;
55+ use serde_json;
5156
5257#[tokio:: main]
5358async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
5459 let publish_key = " my_publish_key" ;
5560 let subscribe_key = " my_subscribe_key" ;
56-
5761 let client = PubNubClientBuilder :: with_reqwest_transport ()
58- . with_keyset (Keyset {
59- subscribe_key ,
60- publish_key : Some (publish_key ),
61- secret_key : None ,
62- })
63- . with_user_id (" user_id" )
64- . build ()? ;
65-
62+ . with_keyset (Keyset {
63+ subscribe_key ,
64+ publish_key : Some (publish_key ),
65+ secret_key : None ,
66+ })
67+ . with_user_id (" user_id" )
68+ . build ()? ;
69+ println! (" PubNub instance created" );
70+
71+ let subscription = client
72+ . subscribe ()
73+ . channels ([" my_channel" . into ()]. to_vec ())
74+ . execute ()? ;
75+
76+ println! (" Subscribed to channel" );
77+
78+ // Launch a new task to print out each received message
79+ tokio :: spawn (subscription . stream (). for_each (| event | async move {
80+ match event {
81+ SubscribeStreamEvent :: Update (update ) => {
82+ match update {
83+ Update :: Message (message ) | Update :: Signal (message ) => {
84+ // Silently log if UTF-8 conversion fails
85+ if let Ok (utf8_message ) = String :: from_utf8 (message . data. clone ()) {
86+ if let Ok (cleaned ) = serde_json :: from_str :: <String >(& utf8_message ) {
87+ println! (" message: {}" , cleaned );
88+ }
89+ }
90+ }
91+ Update :: Presence (presence ) => {
92+ println! (" presence: {:?}" , presence )
93+ }
94+ Update :: Object (object ) => {
95+ println! (" object: {:?}" , object )
96+ }
97+ Update :: MessageAction (action ) => {
98+ println! (" message action: {:?}" , action )
99+ }
100+ Update :: File (file ) => {
101+ println! (" file: {:?}" , file )
102+ }
103+ }
104+ }
105+ SubscribeStreamEvent :: Status (status ) => println! (" \ n status: {:?}" , status ),
106+ }
107+ }));
108+
109+ sleep (Duration :: from_secs (1 )). await ;
110+ // Send a message to the channel
66111 client
67112 . publish_message (" hello world!" )
68113 . channel (" my_channel" )
69114 . r#type (" text-message" )
70115 . execute ()
71116 . await ? ;
72117
118+ sleep (Duration :: from_secs (10 )). await ;
119+
73120 Ok (())
74121}
75122```
@@ -83,22 +130,25 @@ The `pubnub` crate is split into multiple features. You can enable or disable th
83130``` toml
84131# only blocking and access + default features
85132[dependencies ]
86- pubnub = { version = " 0.2.1 " , features = [" blocking" , " access" ] }
133+ pubnub = { version = " 0.3.0 " , features = [" blocking" , " access" ] }
87134
88135# only parse_token + default features
89136[dependencies ]
90- pubnub = { version = " 0.2.1 " , features = [" parse_token" ] }
137+ pubnub = { version = " 0.3.0 " , features = [" parse_token" ] }
91138```
92139
93140### Available features
94141
95142| Feature name | Description | Available PubNub APIs |
96143| :------------ | :---------- | :------------- |
97- | ` full ` | Enables all non-conflicting features | Configuration, Publish, Access Manager, Parse Token |
98- | ` default ` | Enables default features: ` publish ` , ` serde ` , ` reqwest ` , ` aescbc ` , ` std ` | Configuration, Publish |
144+ | ` full ` | Enables all non-conflicting features | Configuration, Publish, Subscribe, Access Manager, Parse Token, Presence |
145+ | ` default ` | Enables default features: ` publish ` , ` subscribe ` , ` serde ` , ` reqwest ` , ` aescbc ` , ` std ` | Configuration, Publish, Subscribe |
99146| ` publish ` | Enables Publish API | Configuration, Publish |
100147| ` access ` | Enables Access Manager API | Configuration, Access Manager |
101148| ` parse_token ` | Enables parsing Access Manager tokens | Configuration, Parse Token |
149+ | ` subscribe ` | Enables Subscribe API | Configuration, Subscribe |
150+ | ` presence ` | Enables Presence API | Configuration, Presence |
151+ | ` tokio ` | Enables the [ tokio] ( https://tokio.rs/ ) asynchronous runtime for Subscribe and Presence APIs | n/a |
102152| ` serde ` | Uses [ serde] ( https://github.com/serde-rs/serde ) for serialization | n/a |
103153| ` reqwest ` | Uses [ reqwest] ( https://github.com/seanmonstar/reqwest ) as a transport layer | n/a |
104154| ` blocking ` | Enables blocking executions of APIs | n/a |
@@ -121,7 +171,7 @@ features and enable the ones you need, for example:
121171
122172``` toml
123173[dependencies ]
124- pubnub = { version = " 0.2.1 " , default-features = false , features = [" serde" , " publish" ,
174+ pubnub = { version = " 0.3.0 " , default-features = false , features = [" serde" , " publish" ,
125175" blocking" ] }
126176```
127177
@@ -135,8 +185,10 @@ some parts of the `alloc::sync` module, which is also not supported in certain `
135185
136186Some SDK features aren't supported in a ` no_std ` environment:
137187
138- * partially ` access ` module (because of lack timestamp support)
188+ * partially ` access ` module (because of lack of timestamp support)
139189* partially ` reqwest ` transport (because of the reqwest implementation details)
190+ * partially ` subscribe ` module (because of the spawning tasks and time dependence)
191+ * partially ` presence ` module (because of the spawning tasks and time dependence)
140192* ` std ` feature (because of the ` std ` library)
141193
142194We depend on a random number generator to generate data for debugging purposes.
0 commit comments