examples/README.md
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 using RedisBloom. |
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 Count-Min Sketch. |
cuckoo-filter.js | Space efficient set membership checks with a Cuckoo Filter using RedisBloom. |
cas-cad-digest.js | Atomic compare-and-set (CAS) and compare-and-delete (CAD) using digests for single-key optimistic concurrency control. |
dump-and-restore.js | Demonstrates the use of the DUMP and RESTORE commands |
get-server-time.js | Get the time from the Redis server. |
hyperloglog.js | Showing use of Hyperloglog commands PFADD, PFCOUNT and PFMERGE. |
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. |
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 to index and search data in hashes. |
search-json.js | Uses RediSearch and RedisJSON to index and search JSON data. |
search-knn.js | Uses RediSearch vector similarity to index and run KNN queries. |
search-hybrid.js | Uses RediSearch hybrid search to combine text search with vector similarity search. |
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 using the XADD command. |
stream-consumer.js | Reads entries from a Redis Stream using the blocking XREAD command. |
time-series.js | Create, populate and query timeseries data with Redis Timeseries. |
topk.js | Use the RedisBloom TopK to track the most frequently seen items. |
stream-consumer-group.js | Reads entries from a Redis Stream as part of a consumer group using the blocking XREADGROUP command. |
transaction-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 with WATCH command on isolated connection with optimistic locking. |
We'd love to see more examples here. If you have an idea that you'd like to see included here, submit a Pull Request and we'll be sure to review it! Don't forget to check out our contributing guide.
To set up the examples folder so that you can run an example / develop one of your own:
git clone https://github.com/redis/node-redis.git
cd node-redis
npm install -ws && npm run build
cd examples
npm install
When adding a new example, please follow these guidelines:
examples folder.js file a meaningful name using - separators e.g. adding-to-a-stream.js / adding-to-a-stream.ts// comment style and comment your code.js / .ts file describing what your example does.js / .ts file describing any Redis commands that need to be run to set up data for your example (try and keep this minimal)async and await'hello' not "hello"foo, bar, baz etc!.js fileREADME.md file to add your example to the tableUse connect-as-acl-user.js as a guide to develop a well formatted example script.
Here's a starter template for adding a new example, imagine this is stored in do-something.js:
// This comment should describe what the example does
// and can extend to multiple lines.
// Set up the data in redis-cli using these commands:
// <add your command(s) here, one per line>
//
// Alternatively, add code that sets up the data.
import { createClient } from 'redis';
const client = createClient();
await client.connect();
// Add your example code here...
client.close();