Skip to content
This repository
Browse code

Add `MONITOR` command and special monitor command reply parsing.

  • Loading branch information...
commit 2fcc947f1114b7e4e463a8fb4fd9196ee3fa50a7 1 parent 1a14e24
Matt Ranney authored
23 README.md
Source Rendered
@@ -394,6 +394,29 @@ of commands and arguments to the constructor:
394 394 });
395 395
396 396
  397 +## Monitor mode
  398 +
  399 +Redis supports the `MONITOR` command, which lets you see all commands received by the Redis server
  400 +across all client connections, including from other client libraries and other computers.
  401 +
  402 +After you send the `MONITOR` command, no other commands are valid on that connection. `node_redis`
  403 +will emit a `monitor` event for every new monitor message that comes across. The callback for the
  404 +`monitor` event takes a timestamp from the Redis server and an array of command arguments.
  405 +
  406 +Here is a simple example:
  407 +
  408 + var client = require("redis").createClient(),
  409 + util = require("util");
  410 +
  411 + client.monitor(function (err, res) {
  412 + console.log("Entering monitoring mode.");
  413 + });
  414 +
  415 + client.on("monitor", function (time, args) {
  416 + console.log(time + ": " + util.inspect(args));
  417 + });
  418 +
  419 +
397 420 # Extras
398 421
399 422 Some other things you might like to know about.
4 changelog.md
Source Rendered
... ... @@ -1,6 +1,10 @@
1 1 Changelog
2 2 =========
3 3
  4 +## v0.5.8 - March 14, 2011
  5 +
  6 +Add `MONITOR` command and special monitor command reply parsing.
  7 +
4 8 ## v0.5.7 - February 27, 2011
5 9
6 10 Add magical auth command.
23 index.js
@@ -48,6 +48,7 @@ function RedisClient(stream, options) {
48 48 this.retry_delay = 250;
49 49 this.retry_backoff = 1.7;
50 50 this.subscriptions = false;
  51 + this.monitoring = false;
51 52 this.closing = false;
52 53 this.server_info = {};
53 54 this.auth_pass = null;
@@ -279,6 +280,7 @@ RedisClient.prototype.connection_gone = function (why) {
279 280 self.connected = false;
280 281 self.ready = false;
281 282 self.subscriptions = false;
  283 + self.monitoring = false;
282 284
283 285 // since we are collapsing end and close, users don't expect to be called twice
284 286 if (! self.emitted_end) {
@@ -361,7 +363,7 @@ RedisClient.prototype.return_error = function (err) {
361 363
362 364 RedisClient.prototype.return_reply = function (reply) {
363 365 var command_obj = this.command_queue.shift(),
364   - obj, i, len, key, val, type;
  366 + obj, i, len, key, val, type, timestamp, args;
365 367
366 368 if (this.subscriptions === false && this.command_queue.length === 0) {
367 369 this.emit("idle");
@@ -414,6 +416,13 @@ RedisClient.prototype.return_reply = function (reply) {
414 416 } else {
415 417 throw new Error("subscriptions are active but got an invalid reply: " + reply);
416 418 }
  419 + } else if (this.monitoring) {
  420 + len = reply.indexOf(" ");
  421 + timestamp = reply.slice(0, len);
  422 + args = reply.slice(len + 1).match(/"[^"]+"/g).map(function (elem) {
  423 + return elem.replace(/"/g, "");
  424 + });
  425 + this.emit("monitor", timestamp, args);
417 426 } else {
418 427 throw new Error("node_redis command queue state error. If you can reproduce this, please report it.");
419 428 }
@@ -469,12 +478,12 @@ RedisClient.prototype.send_command = function () {
469 478 }
470 479 command_obj.sub_command = true;
471 480 this.subscriptions = true;
472   - } else {
473   - if (command === "quit") {
474   - this.closing = true;
475   - } else if (this.subscriptions === true) {
476   - throw new Error("Connection in pub/sub mode, only pub/sub commands may be used");
477   - }
  481 + } else if (command === "monitor") {
  482 + this.monitoring = true;
  483 + } else if (command === "quit") {
  484 + this.closing = true;
  485 + } else if (this.subscriptions === true) {
  486 + throw new Error("Connection in pub/sub mode, only pub/sub commands may be used");
478 487 }
479 488 this.command_queue.push(command_obj);
480 489 this.commands_sent += 1;
2  package.json
... ... @@ -1,5 +1,5 @@
1 1 { "name" : "redis",
2   - "version" : "0.5.7",
  2 + "version" : "0.5.8",
3 3 "description" : "Redis client library",
4 4 "author": "Matt Ranney <mjr@ranney.com>",
5 5 "contributors": [
5 test.js
@@ -1068,7 +1068,8 @@ client.on('end', function () {
1068 1068 ended = true;
1069 1069 });
1070 1070
1071   -// TODO - need a better way to test auth, maybe auto-config a local Redis server?
  1071 +// TODO - need a better way to test auth, maybe auto-config a local Redis server? Sounds hard.
  1072 +// Yes, this is the real password. Please be nice, thanks.
1072 1073 client4.auth("664b1b6aaf134e1ec281945a8de702a9", function (err, res) {
1073 1074 if (err) {
1074 1075 assert.fail(err, name);
@@ -1091,7 +1092,7 @@ client3.on("error", function (err) {
1091 1092 });
1092 1093
1093 1094 client.on("reconnecting", function (params) {
1094   - console.log("reconnecting: " + util.inspect(params));
  1095 +// console.log("reconnecting: " + util.inspect(params));
1095 1096 });
1096 1097
1097 1098 process.on('uncaughtException', function (err) {

0 comments on commit 2fcc947

Please sign in to comment.
Something went wrong with that request. Please try again.