Skip to content

Commit da3face

Browse files
author
Simon Prickett
authored
Add connection status check example and documentation. (#2340)
* Adds example for transactions with arbitrary commands. * Formatting. * Adds isReady doc and example for isReady and isOpen. * Improved example. * Added isOpen explanation. * Removed example from a different PR.
1 parent 118dc11 commit da3face

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ createClient({
6464

6565
You can also use discrete parameters, UNIX sockets, and even TLS to connect. Details can be found in the [client configuration guide](./docs/client-configuration.md).
6666

67+
To check if the the client is connected and ready to send commands, use `client.isReady` which returns a boolean. `client.isOpen` is also available. This returns `true` when the client's underlying socket is open, and `false` when it isn't (for example when the client is still connecting or reconnecting after a network error).
68+
6769
### Redis Commands
6870

6971
There is built-in support for all of the [out-of-the-box Redis commands](https://redis.io/commands). They are exposed using the raw Redis command names (`HSET`, `HGETALL`, etc.) and a friendlier camel-cased version (`hSet`, `hGetAll`, etc.):

examples/README.md

+27-25
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,33 @@
22

33
This folder contains example scripts showing how to use Node Redis in different scenarios.
44

5-
| File Name | Description |
6-
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
7-
| `blocking-list-pop.js` | Block until an element is pushed to a list |
8-
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) |
9-
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
10-
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
11-
| `connect-to-cluster.js` | Connect to Redis cluster |
12-
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch |
13-
| `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io) |
14-
| `get-server-time.js` | Get the time from the Redis server |
15-
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog) |
16-
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
17-
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
18-
| `pubsub-publisher.js` | Adds multiple messages on 2 different channels messages to Redis |
19-
| `pubsub-subscriber.js` | Reads messages from channels using `PSUBSCRIBE` command |
20-
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
21-
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
22-
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
23-
| `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality |
24-
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
25-
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
26-
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |
27-
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
28-
| `stream-consumer-group.js` | Reads entties from a [Redis Stream](https://redis.io/topics/streams-intro) as part of a consumer group using the blocking `XREADGROUP` command |
29-
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking |
5+
| File Name | Description |
6+
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
7+
| `blocking-list-pop.js` | Block until an element is pushed to a list. |
8+
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io). |
9+
| `check-connection-status.js` | Check the client's connection status. |
10+
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers. |
11+
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user. |
12+
| `connect-to-cluster.js` | Connect to a Redis cluster. |
13+
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch. |
14+
| `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io). |
15+
| `get-server-time.js` | Get the time from the Redis server. |
16+
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog). |
17+
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys. |
18+
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/). |
19+
| `pubsub-publisher.js` | Adds multiple messages on 2 different channels messages to Redis. |
20+
| `pubsub-subscriber.js` | Reads messages from channels using `PSUBSCRIBE` command. |
21+
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes. |
22+
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data. |
23+
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality. |
24+
| `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality. |
25+
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command. |
26+
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command. |
27+
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io). |
28+
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
29+
| `stream-consumer-group.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) as part of a consumer group using the blocking `XREADGROUP` command. |
30+
| `tranaaction-with-arbitrary-commands.js` | Mix and match supported commands with arbitrary command strings in a Redis transaction. |
31+
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking. |
3032

3133
## Contributing
3234

examples/check-connection-status.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Check the connection status of the Redis client instance.
2+
import { createClient } from 'redis';
3+
4+
const client = createClient();
5+
6+
console.log('Before client.connect()...');
7+
8+
// isOpen will return False here as the client's socket is not open yet.
9+
// isReady will return False here, client is not yet ready to use.
10+
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);
11+
12+
// Begin connection process...
13+
const connectPromise = client.connect();
14+
15+
console.log('After client.connect()...');
16+
17+
// isOpen will return True here as the client's socket is open now.
18+
// isReady will return False here as the promise hasn't resolved yet.
19+
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);
20+
21+
await connectPromise;
22+
console.log('Afer connectPromise has resolved...');
23+
24+
// isOpen will return True here as the client's socket is open now.
25+
// isReady will return True here, client is ready to use.
26+
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);
27+
28+
await client.quit();

0 commit comments

Comments
 (0)