psubscribe.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. var redis = require("redis"),
  2. client1 = redis.createClient(),
  3. client2 = redis.createClient(),
  4. client3 = redis.createClient(),
  5. client4 = redis.createClient(),
  6. msg_count = 0;
  7. redis.debug_mode = false;
  8. client1.on("psubscribe", function (pattern, count) {
  9. console.log("client1 psubscribed to " + pattern + ", " + count + " total subscriptions");
  10. client2.publish("channeltwo", "Me!");
  11. client3.publish("channelthree", "Me too!");
  12. client4.publish("channelfour", "And me too!");
  13. });
  14. client1.on("punsubscribe", function (pattern, count) {
  15. console.log("client1 punsubscribed from " + pattern + ", " + count + " total subscriptions");
  16. client4.end();
  17. client3.end();
  18. client2.end();
  19. client1.end();
  20. });
  21. client1.on("pmessage", function (pattern, channel, message) {
  22. console.log("("+ pattern +")" + " client1 received message on " + channel + ": " + message);
  23. msg_count += 1;
  24. if (msg_count === 3) {
  25. client1.punsubscribe();
  26. }
  27. });
  28. client1.psubscribe("channel*");