A simple Redis Pub/Sub Client
- Provides a simple layer between the official redis client and the redis pub/sub functionality
Install the Redis Pub/Sub client:
npm install redis-pubsub-client
Require the Redis Pub/Sub client somewhere in your code:
const RedisPubSub = require('redis-pubsub-client');
Start by defining a redis config:
const config = {
host: '127.0.0.1' // Redis server hostname/ip (optional)
port: 32768, // Redis server port (optional)
scope: 'test' // Global message scope (optional)
};
Now construct a new RedisClient like so:
const redisClient = new RedisPubSub(config);
Connect to the redis server:
const waitForRedis = async () => {
await redisClient.connect();
console.log('server ready');
};
waitForRedis();
You are now ready to subscribe to channels and publish messages!
How to subscribe to a channel?:
// Returns the unsubscribe function along the current subscribed channel
const exampleChannel = redisClient.subscribe('example', (data) => {
console.log(data);
});
How to send a message to a channel?:
// Returns true/false based on the fact if the message was recieved by the server
const messageStatus = redisClient.publish('example', {
message: 'Hello World'
});
To catch all Redis server errors use the following function:
redisClient.error((e) => {
console.log(e);
});
To cleanly disconnect from the Redis server use this function:
redisClient.disconnect();
If you need to exit immediately you can also use this function (This is not recommended since this will discard messages that are still in sending state)
redisClient.end();
MIT