You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
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.
This commit is contained in:
@@ -64,6 +64,8 @@ createClient({
|
||||
|
||||
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).
|
||||
|
||||
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).
|
||||
|
||||
### Redis Commands
|
||||
|
||||
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.):
|
||||
|
@@ -3,30 +3,32 @@
|
||||
This folder contains example scripts showing how to use Node Redis in different scenarios.
|
||||
|
||||
| File Name | Description |
|
||||
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `blocking-list-pop.js` | Block until an element is pushed to a list |
|
||||
| `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 |
|
||||
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog) |
|
||||
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
|
||||
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
|
||||
| `pubsub-publisher.js` | Adds multiple messages on 2 different channels messages to Redis |
|
||||
| `pubsub-subscriber.js` | Reads messages from channels using `PSUBSCRIBE` command |
|
||||
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
|
||||
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
|
||||
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
|
||||
| `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality |
|
||||
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
|
||||
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
|
||||
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io) |
|
||||
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `blocking-list-pop.js` | Block until an element is pushed to a list. |
|
||||
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io). |
|
||||
| `check-connection-status.js` | Check the client's connection status. |
|
||||
| `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 a 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. |
|
||||
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog). |
|
||||
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys. |
|
||||
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/). |
|
||||
| `pubsub-publisher.js` | Adds multiple messages on 2 different channels messages to Redis. |
|
||||
| `pubsub-subscriber.js` | Reads messages from channels using `PSUBSCRIBE` command. |
|
||||
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes. |
|
||||
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data. |
|
||||
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality. |
|
||||
| `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality. |
|
||||
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command. |
|
||||
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command. |
|
||||
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io). |
|
||||
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
|
||||
| `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 |
|
||||
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking |
|
||||
| `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. |
|
||||
| `tranaaction-with-arbitrary-commands.js` | Mix and match supported commands with arbitrary command strings in a Redis transaction. |
|
||||
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking. |
|
||||
|
||||
## Contributing
|
||||
|
||||
|
28
examples/check-connection-status.js
Normal file
28
examples/check-connection-status.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// Check the connection status of the Redis client instance.
|
||||
import { createClient } from 'redis';
|
||||
|
||||
const client = createClient();
|
||||
|
||||
console.log('Before client.connect()...');
|
||||
|
||||
// isOpen will return False here as the client's socket is not open yet.
|
||||
// isReady will return False here, client is not yet ready to use.
|
||||
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);
|
||||
|
||||
// Begin connection process...
|
||||
const connectPromise = client.connect();
|
||||
|
||||
console.log('After client.connect()...');
|
||||
|
||||
// isOpen will return True here as the client's socket is open now.
|
||||
// isReady will return False here as the promise hasn't resolved yet.
|
||||
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);
|
||||
|
||||
await connectPromise;
|
||||
console.log('Afer connectPromise has resolved...');
|
||||
|
||||
// isOpen will return True here as the client's socket is open now.
|
||||
// isReady will return True here, client is ready to use.
|
||||
console.log(`client.isOpen: ${client.isOpen}, client.isReady: ${client.isReady}`);
|
||||
|
||||
await client.quit();
|
Reference in New Issue
Block a user