showing results for - "redis pub or sub nodejs"
Deana
03 Apr 2016
1var redis = require(“redis”);var publisher = redis.createClient();publisher.publish(“notification”, “{\”message\”:\”Hello world from Asgardian!\”}”, function(){ process.exit(0);});
Silvia
11 Jan 2020
1more example redis pub/sub -> https://github.com/restuwahyu13/express-todo-redis
Abree
21 May 2020
1// publisher
2
3const IORedis = require('ioredis')
4
5class Publisher {
6	constructor(configs = { key: '' }) {
7		this.key = configs.key
8		Publisher.set(configs.key)
9	}
10
11	static get() {
12		return this.key
13	}
14
15	static set(key = '') {
16		this.key = key
17	}
18
19	_redisConnect() {
20		const ioRedis = new IORedis({
21			host: '127.0.0.1',
22			port: 6379,
23			maxRetriesPerRequest: 50,
24			connectTimeout: 5000,
25			enableReadyCheck: true,
26			enableAutoPipelining: true
27		})
28
29		return ioRedis
30	}
31
32	async setString(keyName = '', data) {
33		const ioRedis = _redisConnect()
34		await ioRedis.set(keyName, data)
35	}
36
37	async setMap(keyName = '', data = {}) {
38		const ioRedis = this._redisConnect()
39		await ioRedis.hmset(keyName, { ...data })
40	}
41
42	async setArray(keyName = '', data = []) {
43		const ioRedis = _redisConnect()
44		await ioRedis.hmset(keyName, JSON.stringify({ data: data }))
45	}
46
47	async setResponse(data = {}) {
48		const ioRedis = this._redisConnect()
49		await ioRedis.hmset('message:speaker', { ...data })
50	}
51}
52
53module.exports = { Publisher }
Charlotte
09 Aug 2018
1// subscriber
2
3const IORedis = require('ioredis')
4const chalk = require('chalk')
5const { Publisher } = require('./util.publisher')
6
7class Subscriber {
8	constructor(configs = { key: '' }) {
9		this._key = configs.key
10		this._keyFrom = Publisher.get()
11	}
12
13	_redisConnect() {
14		const ioRedis = new IORedis({
15			host: '127.0.0.1',
16			port: 6379,
17			maxRetriesPerRequest: 50,
18			connectTimeout: 5000,
19			enableReadyCheck: true,
20			enableAutoPipelining: true
21		})
22
23		return ioRedis
24	}
25
26	async getString(keyName) {
27		if (this._key == this._keyFrom) {
28			const ioRedis = this._redisConnect()
29			const response = await ioRedis.get(keyName)
30			await ioRedis.expire(keyName, 60)
31			if (response) {
32				return Promise.resolve(response)
33			}
34			return {}
35		} else {
36			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
37		}
38	}
39
40	async getMap(keyName) {
41		if (this._key == this._keyFrom) {
42			const ioRedis = this._redisConnect()
43			const response = await ioRedis.hgetall(keyName)
44			await ioRedis.expire(keyName, 60)
45			if (response) {
46				return Promise.resolve(response)
47			}
48			return {}
49		} else {
50			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
51		}
52	}
53
54	async getArray(keyName) {
55		if (this._key == this._keyFrom) {
56			const ioRedis = this._redisConnect()
57			const response = await ioRedis.hgetall(keyName)
58			await ioRedis.expire(keyName, 60)
59			if (response) {
60				return Promise.resolve(JSON.parser(response).data)
61			}
62			return {}
63		} else {
64			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
65		}
66	}
67
68	async getResponse() {
69		if (this._key == this._keyFrom) {
70			const ioRedis = this._redisConnect()
71			const response = await ioRedis.hgetall('message:speaker')
72			await ioRedis.expire('message:speaker', 30)
73			if (response) {
74				return Promise.resolve(response)
75			}
76			return {}
77		} else {
78			return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this._key} and Publisher: ${this._keyFrom}`)))
79		}
80	}
81}
82
83module.exports = { Subscriber }
Hélène
24 Sep 2020
1var 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”);