-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgettingStarted.js
137 lines (101 loc) · 4.78 KB
/
gettingStarted.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require('dotenv').config();
const Ioredis = require('ioredis');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
if (!process.env.REDIS_HOST) {
throw Error('You should first fill the .env-example file and rename it to .env');
}
// Connect to Redis server
console.log();
console.log('🔌 Connecting to Redis...');
console.log();
const redis = new Ioredis({
host: process.env.REDIS_HOST,
port: 6380, // This is the TLS port (communications will be encrypted)
password: process.env.REDIS_PASSWORD,
db: 0,
tls: {} // To activate TLS support (encryption)
});
// This is a good practice to close Redis connection when the Node.js process receives the signal "TERM".
process.once('SIGTERM', () => redis.disconnect());
// SET/GET/DEL examples
console.log('-'.repeat(80));
console.log('🌟 SET/GET/DEL examples');
console.log();
console.log('➡️ Setting key "stackhero-example-key" to value "abcd"');
await redis.set('stackhero-example-key', 'abcd');
console.log();
console.log('➡️ Getting key "stackhero-example-key" value...');
const value = await redis.get('stackhero-example-key');
console.log(`⬅️ Key "stackhero-example-key" has value "${value}"`);
console.log();
console.log('➡️ Deleting key "stackhero-example-key"');
await redis.del('stackhero-example-key');
console.log();
// SADD/SMEMBERS examples
console.log('-'.repeat(80));
console.log('🌟 SADD/SMEMBERS examples');
console.log();
console.log('➡️ Add values "value1", "value2" and "value3" to the set key "stackhero-example-set"');
await redis.sadd('stackhero-example-set', [ 'value1', 'value2', 'value3' ]);
console.log();
console.log('➡️ Getting members from the set key "stackhero-example-set"');
const values = await redis.smembers('stackhero-example-set');
console.log(`⬅️ Set key "stackhero-example-set" has values "${values}"`);
console.log();
// PUB/SUB examples
console.log('-'.repeat(80));
console.log('🌟 PUB/SUB examples');
console.log();
console.log('➡️ [redis] Subscribing to "users" and "events"');
await redis.subscribe([ 'users', 'events' ]);
console.log();
// Listening to messages
redis.on('message', (channel, message) => {
console.log(`⬅️ [redis] Receive message "${message}" from channel "${channel}"`);
console.log();
});
// As we "subscribe" to channels, our "redis" client can't be use anymore for other commands than SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE, PUNSUBSCRIBE, PING and QUIT.
// We create a new "redisPub" client with the same connection options from the "redis" one.
const redisPub = redis.duplicate();
// This is a good practice to close Redis connection when the Node.js process receives the signal "TERM".
process.once('SIGTERM', () => redisPub.disconnect());
console.log('➡️ [redisPub] Sending message to channel "users"');
await redisPub.publish('users', 'I\'m a new user!');
// We wait 200ms to be sure than the message has been received. It just for the demo, to have the "Sending message" and "Receive message" one after the other.
await delay(200);
console.log('➡️ [redisPub] Sending message to channel "events"');
await redisPub.publish('events', 'Here is a new event!');
// We wait 200ms to be sure than the message has been received. It just for the demo, to have the "Sending message" and "Receive message" one after the other.
await delay(200);
console.log('-'.repeat(80));
console.log('👋 Disconnecting "redis" and "redisPub" clients');
await redis.disconnect();
await redisPub.disconnect();
console.log();
// console.log(`➡️ Sending to "users" and "messages"`);
// redis.publish('', "Hello world!");
// // ADD PUB/SUB EXAMPLES
// redis.subscribe("news", "music", function(err, count) {
// // Now we are subscribed to both the 'news' and 'music' channels.
// // `count` represents the number of channels we are currently subscribed to.
// pub.publish("news", "Hello world!");
// pub.publish("music", "Hello again!");
// });
// redis.on("message", function(channel, message) {
// // Receive message Hello world! from channel news
// // Receive message Hello again! from channel music
// console.log("Receive message %s from channel %s", message, channel);
// });
// // There's also an event called 'messageBuffer', which is the same as 'message' except
// // it returns buffers instead of strings.
// redis.on("messageBuffer", function(channel, message) {
// // Both `channel` and `message` are buffers.
// });
// You will get a lot of others examples on the ioredis repository: https://github.com/luin/ioredis
})().catch(error => {
console.error('');
console.error('🐞 An error occurred!');
console.error(error);
process.exit(1);
});