From 1eed12ec65d5bb8b606e41ff5993f40a5847957e Mon Sep 17 00:00:00 2001 From: Varad Karpe Date: Wed, 19 Oct 2022 21:02:07 +0900 Subject: [PATCH] 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 Closes #2281. --- examples/README.md | 1 + examples/connect-to-cluster.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/connect-to-cluster.js diff --git a/examples/README.md b/examples/README.md index 83bbe75438e..b972549202f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -8,6 +8,7 @@ This folder contains example scripts showing how to use Node Redis in different | `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) | | `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers | | `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user | +| `connect-to-cluster.js` | Connect to Redis cluster | | `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch | | `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io) | | `get-server-time.js` | Get the time from the Redis server | diff --git a/examples/connect-to-cluster.js b/examples/connect-to-cluster.js new file mode 100644 index 00000000000..98655497c9e --- /dev/null +++ b/examples/connect-to-cluster.js @@ -0,0 +1,33 @@ +// This is an example script to connect to a running cluster. +// After connecting to the cluster the code sets and get a value. + +// To setup this cluster you can follow the guide here: +// https://redis.io/docs/manual/scaling/ +// In this guide the ports which are being used are 7000 - 7005 + + +import { createCluster } from 'redis'; + +const cluster = createCluster({ + rootNodes : [ + { + url : 'redis://127.0.0.1:7001' + }, + { + url : 'redis://127.0.0.1:7002' + }, + { + url : 'redis://127.0.0.1:7003' + } + ] +}); + +cluster.on('error', (err) => console.log('Redis Client Error', err)); + +await cluster.connect(); + +await cluster.set('hello', 'cluster'); +const value = await cluster.get('hello'); +console.log(value); + +await cluster.quit();