Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use message Middleware in Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to use the message middleware in Redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In Redis, publishers and subscribers of messages cannot communicate directly, but through channels. The publisher of the message sends the message to the specified channel, and after subscribing to the channel, the subscriber of the message receives all messages received in that channel.

In order to implement publish and subscribe, Redis provides a lot of new commands. Let's describe the commands related to publish and subscribe in detail below.

Command

Publish a message

Publish channel message

The return value of the publish command is the number of subscriptions to the channel. Because the channel has no subscribers, the code in the figure above returns a value of 0.

Subscribe to messages

Subscribe channel [channel...]

After the subscribe command is executed successfully, the command line blocks, waiting for a new message to be sent at any time. If we are sending a message to the channel at this time, the subscription will immediately return the message we sent.

Because the channel already has a subscriber, the result returned when we execute the publish command in the figure above is 1.

Let's take a look at the considerations for subscription commands.

The client will enter the subscription state after executing the subscription command, and only four commands such as subscribe, psubscribe, unsubscribe and punsubscribe are allowed.

Newly opened subscription clients cannot receive messages from previous channels because Redis does not persist published messages.

Unsubscribe

Unsubscribe [channel [channel...]]

Subscribe and unsubscribe according to mode

Psubscribe pattern [pattern...]

Punsubscribe [pattern [pattern...]]

Query subscription

View active channels

Pubsub channels

The active channel above means that there must be at least one subscriber.

View the number of channel subscriptions

Pubsub numsub

View the number of mode subscriptions

Pubsub numpat

The above content is the whole content of publishing and subscribing in Redis, which is in proportion to professional messaging middleware such as Kafka, RocketMQ and so on. It does not support message accumulation and backtracking, if we can tolerate the above shortcomings when using the publish and subscribe function, then the publish and subscribe function in our Redis can be given priority.

Thank you for reading! This is the end of this article on "how to use message middleware in Redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report