File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,20 @@ enum Command {
39
39
#[ structopt( parse( try_from_str = duration_from_ms_str) ) ]
40
40
expires : Option < Duration > ,
41
41
} ,
42
+ /// Publisher to send a message to a specific channel.
43
+ Publish {
44
+ /// Name of channel
45
+ channel : String ,
46
+
47
+ #[ structopt( parse( from_str = bytes_from_str) ) ]
48
+ /// Message to publish
49
+ message : Bytes ,
50
+ } ,
51
+ /// Subscribe a client to a specific channel or channels.
52
+ Subscribe {
53
+ /// Specific channel or channels
54
+ channels : Vec < String > ,
55
+ } ,
42
56
}
43
57
44
58
/// Entry point for CLI tool.
@@ -93,6 +107,24 @@ async fn main() -> mini_redis::Result<()> {
93
107
client. set_expires ( & key, value, expires) . await ?;
94
108
println ! ( "OK" ) ;
95
109
}
110
+ Command :: Publish { channel, message } => {
111
+ client. publish ( & channel, message. into ( ) ) . await ?;
112
+ println ! ( "Publish OK" ) ;
113
+ }
114
+ Command :: Subscribe { channels } => {
115
+ if channels. is_empty ( ) {
116
+ return Err ( "channel(s) must be provided" . into ( ) ) ;
117
+ }
118
+ let mut subscriber = client. subscribe ( channels) . await ?;
119
+
120
+ // await messages on channels
121
+ while let Some ( msg) = subscriber. next_message ( ) . await ? {
122
+ println ! (
123
+ "got message from the channel: {}; message = {:?}" ,
124
+ msg. channel, msg. content
125
+ ) ;
126
+ }
127
+ }
96
128
}
97
129
98
130
Ok ( ( ) )
You can’t perform that action at this time.
0 commit comments