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 {
3939 #[ structopt( parse( try_from_str = duration_from_ms_str) ) ]
4040 expires : Option < Duration > ,
4141 } ,
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+ } ,
4256}
4357
4458/// Entry point for CLI tool.
@@ -93,6 +107,24 @@ async fn main() -> mini_redis::Result<()> {
93107 client. set_expires ( & key, value, expires) . await ?;
94108 println ! ( "OK" ) ;
95109 }
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+ }
96128 }
97129
98130 Ok ( ( ) )
You can’t perform that action at this time.
0 commit comments