[toc]
Redis 發布訂閱是一種消息通信模式:發送者發送消息,訂閱者接收消息,客戶端可以訂閱任意數量的頻道。

## 命令行操作

## 用node操作
```
const redis = require('redis');
let client1 = redis.createClient(6369,'localhost');
let client2 = redis.createClient(6369,'localhost');
//client1訂閱了food drink兩個頻道
client1.subscribe('food');
client1.subscribe('drink');
//指定當收到訂閱的消息之后要干什么
client1.on('message',function(channel,message){
console.log(channel,message);
client.unsubscribe('food'); //以后再也不接受食品頻道的消息
});
//client2向頻道food 和 drink發送 消息
//監聽比較慢 需要延時發送
setTimeout(function(){
client2.publish('food','面包');
client2.publish('drink','可樂');
setTimeout(function(){
client2.publish('food','面包2');
client2.publish('drink','可樂2');
},1000);
},1000);
```