abstract:redis的消息發(fā)布與訂閱適合做在線聊天, 消息推送使用方法:發(fā)布端: publish +頻道名稱 + 發(fā)布內(nèi)容訂閱端: subscribe + 頻道名稱發(fā)布端例子:127.0.0.1:6379> publish news 'this is a test' (integer) 0 127.0.0.1:637
redis的消息發(fā)布與訂閱適合做在線聊天, 消息推送
使用方法:
發(fā)布端: publish +頻道名稱 + 發(fā)布內(nèi)容
訂閱端: subscribe + 頻道名稱
發(fā)布端例子:
127.0.0.1:6379> publish news 'this is a test' (integer) 0 127.0.0.1:6379>
新開一個redis-cli
訂閱端:
127.0.0.1:6379> subscribe news Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "news" 3) (integer) 1
再次在發(fā)布端發(fā)布一個消息
127.0.0.1:6379> publish news 'another test' (integer) 1 #返回的數(shù)字表示目前訂閱這個頻道的數(shù)量 127.0.0.1:6379>
查看訂閱端
127.0.0.1:6379> subscribe news Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "news" 3) (integer) 1 1) "message" 2) "news" 3) "another test"
psubscribe
模糊訂閱, 比如訂閱上例的news, 可以寫成 __psubscribe ne*__ , 表示訂閱所有 ne 開頭的頻道