how to implement redis pub sub model using nodejs
var redis = require(“redis”);var publisher = redis.createClient();publisher.publish(“notification”, “{\”message\”:\”Hello world from Asgardian!\”}”, function(){ process.exit(0);});
how to implement redis pub sub model using nodejs
var redis = require(“redis”);var publisher = redis.createClient();publisher.publish(“notification”, “{\”message\”:\”Hello world from Asgardian!\”}”, function(){ process.exit(0);});
redis pub or sub nodejs
more example redis pub/sub -> https://github.com/restuwahyu13/express-todo-redis
how to implement redis pub sub model using nodejs
var redis = require(“redis”);var subscriber = redis.createClient();subscriber.on(“message”, function (channel, message) { console.log(“Message: “ + message + “ on channel: “ + channel + “ is arrive!”);});subscriber.subscribe(“notification”);
redis pub or sub nodejs
// publisher
const IORedis = require('ioredis')
class Publisher {
constructor(configs = { key: '' }) {
this.key = configs.key
Publisher.set(configs.key)
}
static get() {
return this.key
}
static set(key = '') {
this.key = key
}
_redisConnect() {
const ioRedis = new IORedis({
host: '127.0.0.1',
port: 6379,
maxRetriesPerRequest: 50,
connectTimeout: 5000,
enableReadyCheck: true,
enableAutoPipelining: true
})
return ioRedis
}
async setString(keyName = '', data) {
const ioRedis = _redisConnect()
await ioRedis.set(keyName, data)
}
async setMap(keyName = '', data = {}) {
const ioRedis = this._redisConnect()
await ioRedis.hmset(keyName, { ...data })
}
async setArray(keyName = '', data = []) {
const ioRedis = _redisConnect()
await ioRedis.hmset(keyName, JSON.stringify({ data: data }))
}
async setResponse(data = {}) {
const ioRedis = this._redisConnect()
await ioRedis.hmset('message:speaker', { ...data })
}
}
module.exports = { Publisher }
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us