redis pub or sub nodejs code example

Example 1: redis pub or sub nodejs

more example redis pub/sub -> https://github.com/restuwahyu13/express-todo-redis

Example 2: redis pub or sub nodejs

// subscriber

const IORedis = require('ioredis')
const chalk = require('chalk')
const { Publisher } = require('./util.publisher')

class Subscriber {
	constructor(configs = { key: '' }) {
		this._key = configs.key
		this._keyFrom = Publisher.get()
	}

	_redisConnect() {
		const ioRedis = new IORedis({
			host: '127.0.0.1',
			port: 6379,
			maxRetriesPerRequest: 50,
			connectTimeout: 5000,
			enableReadyCheck: true,
			enableAutoPipelining: true
		})

		return ioRedis
	}

	async getString(keyName) {
		if (this._key == this._keyFrom) {
			const ioRedis = this._redisConnect()
			const response = await ioRedis.get(keyName)
			await ioRedis.expire(keyName, 60)
			if (response) {
				return Promise.resolve(response)
			}
			return {}
		} else {
			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
		}
	}

	async getMap(keyName) {
		if (this._key == this._keyFrom) {
			const ioRedis = this._redisConnect()
			const response = await ioRedis.hgetall(keyName)
			await ioRedis.expire(keyName, 60)
			if (response) {
				return Promise.resolve(response)
			}
			return {}
		} else {
			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
		}
	}

	async getArray(keyName) {
		if (this._key == this._keyFrom) {
			const ioRedis = this._redisConnect()
			const response = await ioRedis.hgetall(keyName)
			await ioRedis.expire(keyName, 60)
			if (response) {
				return Promise.resolve(JSON.parser(response).data)
			}
			return {}
		} else {
			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
		}
	}

	async getResponse() {
		if (this._key == this._keyFrom) {
			const ioRedis = this._redisConnect()
			const response = await ioRedis.hgetall('message:speaker')
			await ioRedis.expire('message:speaker', 30)
			if (response) {
				return Promise.resolve(response)
			}
			return {}
		} else {
			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
		}
	}
}

module.exports = { Subscriber }

Example 3: 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 }

Example 4: 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”);

Example 5: 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);});

Tags:

Misc Example