Skip to content

Commit 1eed12e

Browse files
authored
Connecting to cluster example (#2298)
* Connecting to cluster example * Connecting to cluster example * Making changes according to review * Adding example to Readme.md in alphabetical order * Fixed order of scripts so they are alphabetic. Co-authored-by: Simon Prickett <[email protected]> Closes #2281.
1 parent d0bfa77 commit 1eed12e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This folder contains example scripts showing how to use Node Redis in different
88
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) |
99
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
1010
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
11+
| `connect-to-cluster.js` | Connect to Redis cluster |
1112
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch |
1213
| `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io) |
1314
| `get-server-time.js` | Get the time from the Redis server |

examples/connect-to-cluster.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This is an example script to connect to a running cluster.
2+
// After connecting to the cluster the code sets and get a value.
3+
4+
// To setup this cluster you can follow the guide here:
5+
// https://redis.io/docs/manual/scaling/
6+
// In this guide the ports which are being used are 7000 - 7005
7+
8+
9+
import { createCluster } from 'redis';
10+
11+
const cluster = createCluster({
12+
rootNodes : [
13+
{
14+
url : 'redis://127.0.0.1:7001'
15+
},
16+
{
17+
url : 'redis://127.0.0.1:7002'
18+
},
19+
{
20+
url : 'redis://127.0.0.1:7003'
21+
}
22+
]
23+
});
24+
25+
cluster.on('error', (err) => console.log('Redis Client Error', err));
26+
27+
await cluster.connect();
28+
29+
await cluster.set('hello', 'cluster');
30+
const value = await cluster.get('hello');
31+
console.log(value);
32+
33+
await cluster.quit();

0 commit comments

Comments
 (0)