Although Redis has a subscription function, the subscription function is real-time, and after this point, the message is not received.
At the same time, if the subscribed client is shutdown for some special reason, it will not find a full subscription event.
But fortunately, Redis also has a message queue, through Message Queuing, we can not only make the publication submission faster (the Conference traverse all Subscribers, and notify all subscribers), and may not have to worry about the subscriber missing out the notice of the exception.
I will coordinate subscribers, post offices, and publisher roles in subsequent essays through Swoole,phpredis,yii.
The actual implementation of the PUBLISH command is done by the Pubsubpublishmessage function, which is fully defined as follows:
Send Message int Pubsubpublishmessage (RobJ *channel, RobJ *message) { int receivers = 0; struct Dictentry *de; ListNode *ln; Listiter Li; /* Send to clients listening for that channel */ Send a message to subscribers on all channels de = Dictfind (Server.pubsub_channels,channel); if (DE) { List *list = Dictgetval (DE); Remove all Subscribers ListNode *ln; Listiter Li; Traverse all Subscribers and send messages to them Listrewind (List,&li); while ((ln = listnext (&li)) = NULL) { Redisclient *c = ln->value; Addreply (C,shared.mbulkhdr[3]); Addreply (C,shared.messagebulk); Addreplybulk (C,channel); Print Channel name Addreplybulk (C,message); Print messages receivers++; Update the number of recipients } } /* Send to clients listening to matching channels */ Send a message to all subscribers who are matched to the pattern if (Listlength (server.pubsub_patterns)) { Listrewind (Server.pubsub_patterns,&li); Remove all modes Channel = Getdecodedobject (channel); while ((ln = listnext (&li)) = NULL) { Pubsubpattern *pat = ln->value; Remove mode If the pattern matches the channel, Send a message to subscribers in this mode if (Stringmatchlen (char*) pat->pattern->ptr, Sdslen (PAT->PATTERN->PTR), (char*) Channel->ptr, Sdslen (Channel->ptr), 0)) { Addreply (Pat->client,shared.mbulkhdr[4]); Addreply (Pat->client,shared.pmessagebulk); Addreplybulk (Pat->client,pat->pattern); Print the pattern that is matched Addreplybulk (Pat->client,channel); Print Channel name Addreplybulk (Pat->client,message); Print messages receivers++; Update the number of recipients } } Decrrefcount (channel); Releasing The Used channel } return receivers; Returns the number of recipients }
|
Redis achieves efficient, non-missing event encapsulation