showing results for - "how to implement redis pub sub model using nodejs"
Oakley
25 May 2019
1var redis = require(“redis”);var publisher = redis.createClient();publisher.publish(“notification”, “{\”message\”:\”Hello world from Asgardian!\”}”, function(){ process.exit(0);});
Jacob
26 May 2018
1more example redis pub/sub -> https://github.com/restuwahyu13/express-todo-redis
Celya
07 Feb 2017
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”);
Christian
20 Feb 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 }