I have two redis clients, in one file I have a simple script setting and deleted Redis keys:
var redis = require("redis");
var client = redis.createClient('6379','127.0.0.1');
client.config("SET","notify-keyspace-events", "KEA");
client.set("string key 1", "string val", redis.print);
client.set("string key 2", "string val", redis.print);
client.set("placeholder","placeholder value", redis.print);
client.del("string key 1", redis.print);
client.del("string key", redis.print);
in the second file, I have a Redis client serving as a subscriber:
var redis = require("redis");
var subscriber = redis.createClient('6379','127.0.0.1');
const REDIS_PUB_MESSAGE = 'redis_pub_message';
const EVENT_SET = '__keyevent@0__:set';
const EVENT_DEL = '__keyevent@0__:del';
const SPACE_SPECIFIC_KEY = '__keyspace@0__:placeholder set';
const EVENT_SPECIFIC_KEY = '__keyevent@0__:placeholder set';
const SPACE_SPECIFIC_KEY_set = '__keyspace@0__:set placeholder';
const EVENT_SPECIFIC_KEY_set = '__keyevent@0__:set placeholder';
subscriber.on('message', function(channel, key) {
switch (channel) {
case SPACE_SPECIFIC_KEY_set:
console.log('space specific key channel:',channel,'key:',key);
case EVENT_SPECIFIC_KEY_set:
console.log('event specific key channel:',channel,'key:',key);
case EVENT_SPECIFIC_KEY:
console.log('space specific key channel:',channel,'key:',key);
case SPACE_SPECIFIC_KEY:
console.log('event specific key channel:',channel,'key:',key);
}
});
the key 'placeholder' is being set, so is there any good reason that I am not getting any output in the 'message' handler?
You forgot to subscribe the subscriber client to a specific channel. Furthermore, if you want to watch for all events, you need to use pattern-based subscription.
You may want to do something like this (untested):
See more information in the Redis documentation, and in node_redis examples.
Update:
Here is an example to illustrate the difference between channel subcription and pattern subscription. Proper error handling is omitted for brevity sake.
The result of this script is:
You can see that first subscriber received only events for mykey, while second subscriber receives events for all keys.