From d76c233d927c26653c32f6edad49913c7d4c8a40 Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Tue, 2 Feb 2021 08:18:09 +0000 Subject: [PATCH] simple pubsub test in redis file --- redis.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/redis.js b/redis.js index f37301cf..86ab4638 100644 --- a/redis.js +++ b/redis.js @@ -2,11 +2,32 @@ const Redis = require('ioredis') , configs = require(__dirname+'/configs/main.js') - , client = new Redis(configs.redis); + , client = new Redis(configs.redis) + , publisher = new Redis(configs.redis); + +client.subscribe('config', (err, count) => { + if (err) { + return console.error(err); + } + console.log(`Redis subscribed to ${count} channels`); +}); + +client.on("message", (channel, message) => { + switch (channel) { + case 'config': + //TODO: something, change the configs import to a new config handler class/module + void 0; + break; + default: + console.error(`Unhandled pubsub channel ${channel} message: ${message}`); + break; + } +}); module.exports = { redisClient: client, + redisPublisher: publisher, //get a value with key get: (key) => { @@ -16,15 +37,15 @@ module.exports = { //set a value on key set: (key, value, ttl) => { if (ttl) { - client.set(key, JSON.stringify(value), 'EX', ttl); + return client.set(key, JSON.stringify(value), 'EX', ttl); } else { - client.set(key, JSON.stringify(value)); + return client.set(key, JSON.stringify(value)); } }, //set a value on key if not exist setnx: (key, value) => { - client.setnx(key, JSON.stringify(value)); + return client.setnx(key, JSON.stringify(value)); }, //add items to a set