# Redis Publish - Subscribe **Redis Pub / Sub** được áp dụng trong các hệ thống cần gửi/nhận bản tin message nơi sender (publisher) gửi message trong khi receiver (subscriber) nhận được chúng. Liên kết mà các message được chuyển được gọi là kênh. Trong Redis, khách hàng có thể subscribe bất kỳ số lượng kênh. [![Redis-Pub-Sub-2-768x253.jpeg](http://laptrinh.vn/uploads/images/gallery/2019-10/scaled-1680-/Redis-Pub-Sub-2-768x253.jpeg)](http://laptrinh.vn/uploads/images/gallery/2019-10/Redis-Pub-Sub-2-768x253.jpeg) Ví dụ: - subscriber: 1 Client đăng ký nhận message từ redisChat: ```shell redis 127.0.0.1:6379> SUBSCRIBE redisChat Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChat" 3) (integer) 1 ``` \- publisher: 2 Client gửi message vào channel redisChat: ``` redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique" (integer) 1 redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by Laptrinh.vn" (integer) 1 1) "message" 2) "redisChat" 3) "Redis is a great caching technique" 1) "message" 2) "redisChat" 3) "Learn redis by Laptrinh.vn" ``` **Các command cơ bản:**
STTCommandMô tả
1PSUBSCRIBE pattern \[pattern ...\]Đăng ký (subscibe) channel theo pattern
2PUBSUB subcommand \[argument \[argument ...\]\]Cho biết trạng thái của hệ thống Pub / Sub. Ví dụ, client nào đang hoạt động trên máy chủ.
3PUBLISH channel messageGửi message tới channel
4PUNSUBSCRIBE \[pattern \[pattern ...\]\]Dừng nhận message của channel theo pattern
5SUBSCRIBE channel \[channel ...\]Lắng nghe message của channel
6UNSUBSCRIBE \[channel \[channel ...\]\]Dừng lắng nghe message của channel