Here, using Nodejs's Redis module description, specifically visible Https://www.npmjs.com/package/redis, first come through a simple example of the pub/sub in Redis how to implement it.
var express = require (' Express '); var router = Express. Router (); var Redis = require ("Redis"); /* GET home page. */router.get ('/', function (req, res, next) {var client1 = redis.createclient (); var client2 = redis.createclient (6379, "192.168.20.132"); var msg_count = 0; Client1.on ("Error", function (Err) {Console.log ("error" + err); }); Client2.on ("Error", function (Err) {Console.log (err); }); The Client1.on ("subscribe", function (channel, count) {//channel is the event type (channel) of the subscription, and count is the total number of channels subscribed to by the current client Client2.publish (" A nice channel "," I am sending a message. "); Client2.publish ("A nice Channel", "I am sending a second message."); Client2.publish ("A nice Channel", "I am sending my last message."); Console.log ("Client1 Sub count:" + count); Console.log ("Client1 Sub Channel:" + channel); }); Client1.on ("Message", function (channel, message) {//channel is the type of subscription sent to the send side, and message is the information received in that channel Console.log ("Cl IENT1 Channel Name: "+ Channel +"->message: "+ messAge); Msg_count + = 1; if (Msg_count = = = 3) {client1.unsubscribe (); Unsubscribe all channel client1.end (); CLIENT1 exit Pub/sub mode, can continue to perform other Redis command Client2.end (); } }); Client1.subscribe ("A nice channel"); Auto Trigger "Sunscribe" event Res.render (' index ', {title: ' Express '}); Module.exports = router;
Save the above code as a index.js file, replace the index.js in the Express project, and then enter it in the browser after launching: localhost:3000, observe webstorm print information as follows:
Client1 Sub Count:1client1 Sub channel:a nice channelclient1 channel name:a Nice channel->message:i am sending a mess Age.client1 Channel Name:a Nice channel->message:i am sending a second message.client1 channel name:a Nice channel-& Gt;message:i am sending my last message.
Here to illustrate the following:
- After require to the Redis module, a Redis client connection is created via the Redis CreateClient method, which can specify three parameters: the connected Redis server port, the server IP, and the configurable options. Default connection to the 6379 port of the native Redis server when not with any parameters, the default configuration can also be used with options of createclient (6379, "IP address", {}) when encoding, or you can omit the options object directly
- Event that is automatically triggered when the error event is an error on client side operation
- Subscribe events and Message events are explained later
Publish a subscription?
The publication subscription in Redis, the self-understanding is: Publish a subscription is one end of the message, the end of the subscription message to receive the message, where the publication of the terminal can be called the client side, that is, a client can publish multiple messages, you can also subscribe to multiple messages.
Speaking of news, what is the news? Each message in Redis is a multiple batch reply (multi-bulk-reply) with three elements. It was hard to understand when the goods were just heard. The first element here is the message type, and the message type in Redis is not the string, object, etc. that we understand, but subscribe, unsubscribe, message, and so on.
- Subscribe: If the type is subscribe, the current client successfully subscribes to the channel shown in the second element (the channel can be understood as the name or channel of the message, since the client side of Redis publishes the message, Redis The server forwards the message to the Subscriber based on whether the other client subscribes to the name or channel, and the third element of the message records the total number of channels currently subscribed by the client
- Unsubscribe: Indicates that the current client successfully unsubscribe to the channel indicated by the second element of the message. The third element of the information records the number of channels that the client is still subscribed to. When the number of channels subscribed to by a client drops to a
0
client that no longer subscribes to any channel, it can execute any Redis command as usual
- Message: This information is the real information that is sent by a client executing the Publish command. The second element of information is the channel of the information source, and the third element is the content of the information
If the client executes the following command:
Redis> SUBSCRIBE First Second
Indicates that the client subscribed to the two channel called First and second, then it receives the following reply:
1) "Subscribe" 2) "first" 3) (integer) one) "Subscribe" 2) "second" 3) (integer) 2
Where subscribe indicates this message type, first is the channel name, (integer) 1 indicates that the total number of channel for the current client subscription is 1, and if another client executes the following command at this point:
Redis> PUBLISH Second Hello
Indicates that the client has published a message with a channel of second, with the content Hello, and the client who previously subscribed to channel second will receive the following information:
1) "Message" 2) "second" 3) "Hello"
Where the message indicates that it was sent to another client, second is the channel name, and Hello is the message content. If the subscribed client then executes the following command:
RedisUnsubscribe indicates that subscribers unsubscribe from all previously subscribed channel, and then receive a response after the command executes as follows:
1) "Unsubscribe" 2) "second" 3) (integer) one) "Unsubscribe" 2) "first" 3) (integer) 0
You can see the message when the unsubscribe is a one-off, and is "after the first order to return" sequence, of course, you can also directly specify the channel to unsubscribe.
One thing is more important:
The information sent through the publish is not saved on the server side, the server is just doing the transfer processing. That is, if the client publish the message to the channel, and no other client subscribes to the channel, then the message is not valid and the message is passed as a channel carrier.
Publish subscriptions in Redis (pub/sub)