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):
subscriber.on("pmessage", function (pattern, channel, message) {
console.log("("+ pattern +")" + " client received message on " + channel + ": " + message);
switch (channel) {
// blah blah blah
// ...
}
});
subscriber.psubscribe(''__key*__:*')
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.
var redis = require("redis");
var client = redis.createClient('6379','127.0.0.1');
var subscriber1 = redis.createClient('6379','127.0.0.1');
var subscriber2 = redis.createClient('6379','127.0.0.1');
// First subscriber listens only to events occurring for key mykey
function S1(next) {
subscriber1.on('message', function(channel, msg) {
console.log( "S1: received on "+channel+" event "+msg )
});
subscriber1.subscribe( "__keyspace@0__:mykey", function (err) {
next();
});
}
// Second subscriber listens to events occuring for ALL keys
function S2(next) {
subscriber2.on('pmessage', function(pattern,channel, msg) {
console.log( "S2: received on "+channel+" event "+msg )
});
subscriber2.psubscribe( "__keyspace@0__:*", function (err) {
next();
});
}
// Do something with keys mykey and anotherkey
function do_something() {
client.set("mykey","example", function( err ) {
client.set("mykey", "another example", function( err ) {
client.del("mykey", function( err ) {
client.set("anotherkey","example", function( err ) {
client.del("anotherkey");
});
});
});
});
}
// Here we go
S1( function () {
S2( function () {
do_something();
});
});
The result of this script is:
S1: received on __keyspace@0__:mykey event set
S2: received on __keyspace@0__:mykey event set
S2: received on __keyspace@0__:mykey event set
S1: received on __keyspace@0__:mykey event set
S1: received on __keyspace@0__:mykey event del
S2: received on __keyspace@0__:mykey event del
S2: received on __keyspace@0__:anotherkey event set
S2: received on __keyspace@0__:anotherkey event del
You can see that first subscriber received only events for mykey, while second subscriber receives events for all keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With