当前位置:网站首页>Redis: redis message publishing and subscription (understand)

Redis: redis message publishing and subscription (understand)

2022-07-04 23:05:00 dengfengling999

In order to realize the communication of the client , Provides the concept of channel

1.Redis Publish subscribe

Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Send a message , subscriber (sub) receive messages .Redis Clients can subscribe to any number of channels .

Redis Publish and subscribe diagram

Figure 1 : Message subscribers (client2 client5 and client1) Subscribed Channels channel1

  Figure 2 : The publisher publishes messages to the channel channel1, Will be sent to three subscribers :

 

Redis Common commands for publishing and subscribing

1.subscribe

grammar :subscribe channel [channel…]

function : Subscribe to information about one or more channels

Return value : Subscribed messages

for example :

 

2.publish

grammar :publish chanel message

function : Send the message to the specified channel .

Return value : Numbers . Number of message subscribers received .

 

  Another client :

ch1 Just received  

 

 

3.psubscribe

grammar :psubscribe pattern [pattern]

function : Subscribe to one or more channels that match the given pattern . Mode with  *  As a wildcard , for example :news.* Match all to news. The first channel .

Return value : Subscription information .

because Redis The function of is single , And unstable

It will not be used in actual development , Publish and subscribe to client messages , We will learn a technology that specializes in message communication and publishing  

原网站

版权声明
本文为[dengfengling999]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042243410815.html