Skip to content

Commit 7615f74

Browse files
author
Simon Prickett
authored
Merge pull request #184 from redis-developer/update-node-getting-started-node-redis-4
Updated Node intro for node-redis 4 and to make node-redis the primary choice.
2 parents edba61f + 2c76167 commit 7615f74

File tree

1 file changed

+73
-57
lines changed

1 file changed

+73
-57
lines changed

docs/develop/node/gettingstarted/index-gettingstarted.mdx

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ id: index-gettingstarted
33
title: Getting Started with Node.js
44
sidebar_label: Getting Started
55
slug: /develop/node/gettingstarted
6+
authors: [ajeet, simon]
67
---
78

89
import Tabs from '@theme/Tabs';
@@ -14,99 +15,114 @@ Find tutorials, examples and technical articles that will help you to develop wi
1415

1516
## Introduction
1617

17-
Redis is a great database for use with Node.js. Both Redis Node.js share similar type conventions and threading models, which makes for a very predictable development experience. By pairing Node.js & Redis together you can achieve a scalable and productive development platform.
18+
Redis is a great database for use with Node.js. Both Redis and Node.js share similar type conventions and threading models, which makes for a very predictable development experience. By pairing Node.js and Redis together you can achieve a scalable and productive development platform.
1819

19-
Redis Node.js has two primary clients available [node_redis](https://github.com/NodeRedis/node_redis) and [ioredis](https://github.com/luin/ioredis). Both are available through npm. Both clients have pros and cons, but we generally suggest node_redis, as it has wide support for Redis modules, it is easily extensible and widely used. Ioredis has better integrated support for the Redis Enterprise Cluster API, but this is only relevant if you're specifically using [the Cluster API topology](https://redis.com/redis-enterprise/technology/linear-scaling-redis-enterprise/) instead of the more common Proxy topology. Node_redis’ support for the Cluster API is provided by an additional shim, redis-clustr. Node_redis has built-in support for Redis Enterprise features such as [Active Active geo distribution](https://redis.com/redis-enterprise/technology/active-active-geo-distribution/), allowing you to have one database that spans multiple clusters. Node_redis also supports Redis-on-Flash which enables extending your database into Flash SSD storage for warm values and DRAM for hot values. Nodejs redis enables you to integrate such features.
20+
Redis has two primary Node.js clients which are [node-redis](https://github.com/redis/node-redis) and [ioredis](https://github.com/luin/ioredis). Both are available through npm. We generally suggest using node-redis, as it has wide support for Redis modules, is easily extended, and is widely used.
2021

21-
The Node.js community has built many client libraries that you can find [here](https://redis.io/clients#nodejs).
22-
For your first steps with Node.js and Redis, this article will show how to use the recommended libraries: [ioredis](https://github.com/luin/ioredis) and [node-redis](https://github.com/NodeRedis/node-redis).
22+
Check out a list of Redis clients that the community has built for Node.js [here](https://redis.io/clients#nodejs).
2323

24+
This article shows how to get started with the recommended libraries: [node-redis](https://github.com/redis/node-redis) and [ioredis](https://github.com/luin/ioredis).
2425
<Tabs
25-
defaultValue="ioredis"
26+
defaultValue="node-redis"
2627
values={[
27-
{label: 'IO redis', value: 'ioredis'},
28-
{label: 'Node Redis', value: 'node_redis'},
28+
{label: 'node-redis', value: 'node-redis'},
29+
{label: 'ioredis', value: 'ioredis'},
2930
]}>
30-
<TabItem value="ioredis">
31+
<TabItem value="node-redis">
3132

3233

33-
#### Step 1. Install the ioredis library using `npm` (or `yarn`)
34+
#### Step 1. Install node-redis using `npm` (or `yarn`)
3435

3536
```bash
36-
npm install ioredis
37+
npm install redis
3738
```
3839

3940

40-
#### Step 2. Write your application code
41+
#### Step 2. Write your Application Code
4142

42-
```javascript
43-
const Redis = require("ioredis");
44-
const redis = new Redis(6379, "localhost");
43+
```javascript
44+
import { createClient } from 'redis';
4545

46-
redis.set("mykey", "Hello from io-redis!");
47-
redis.get("mykey", function(err, result){
48-
if (err) { throw err; }
49-
50-
console.log(result);
51-
});
46+
async function nodeRedisDemo () {
47+
try {
48+
const client = createClient();
49+
await client.connect();
5250

53-
// use the promise instead of callback
54-
redis.get("mykey").then(function (result) {
55-
console.log(result);
56-
});
51+
await client.set('mykey', 'Hello from node-redis!');
52+
const myKeyValue = await client.get('mykey');
53+
console.log(myKeyValue);
5754

58-
redis.zadd("vehicles", 0, "car", 0, "bike");
59-
redis.zrange("vehicles", 0, -1, "WITHSCORES").then((res) => console.log(res));
55+
const numAdded = await client.zAdd('vehicles', [
56+
{
57+
score: 4,
58+
value: 'car'
59+
},
60+
{
61+
score: 2,
62+
value: 'bike'
63+
}
64+
]);
65+
console.log(`Added ${numAdded} items.`);
6066

61-
```
67+
for await (const { score, value } of client.zScanIterator('vehicles')) {
68+
console.log(`${value} -> ${score}`);
69+
}
6270

63-
Find more information about Redis & io-redis connections in the "[Redis Connect](https://github.com/redis-developer/redis-connect/tree/master/node.js/ioredis)" repository.
71+
await client.quit();
72+
} catch (e) {
73+
console.error(e);
74+
}
75+
}
76+
77+
nodeRedisDemo();
78+
```
6479

6580
</TabItem>
66-
<TabItem value="node_redis">
81+
<TabItem value="ioredis">
6782

6883

69-
#### Step 1. Install the Redis library using `npm` (or `yarn`)
84+
#### Step 1. Install ioredis using `npm` (or `yarn`)
7085

7186
```bash
72-
npm install redis
87+
npm install ioredis
7388
```
7489

7590

76-
#### Step 2. Write your application code
91+
#### Step 2. Write your Application Code
7792

78-
```javascript
79-
const redis = require("redis");
80-
81-
const client = redis.createClient ({
82-
port : 6379,
83-
host : "localhost"
84-
});
93+
```javascript
94+
const Redis = require('ioredis');
8595

86-
client.set("mykey", "Hello from node-redis!");
87-
client.get("mykey", function(err, result){
88-
if (err) { throw err; }
89-
90-
console.log(result);
91-
});
96+
async function ioredisDemo() {
97+
try {
98+
const client = new Redis();
9299

93-
const vehiclesData = ["vehicles", 4, "car", 2, "bike"];
94-
client.zadd(vehiclesData, function(addError, addResponse) {
95-
if (addError) { throw addError; }
100+
await client.set('mykey', 'Hello from io-redis!');
101+
const myKeyValue = await client.get('mykey');
102+
console.log(myKeyValue);
96103

97-
console.log("added " + addResponse + " items.");
104+
const numAdded = await client.zadd('vehicles', 4, 'car', 2, 'bike');
105+
console.log(`Added ${numAdded} items.`);
98106

99-
const query = ["vehicles", 0, -1];
100-
client.zrange(query, function(rangeError, rangeResponse) {
101-
if (rangeError) {throw rangeError;}
102-
console.log("vehicles", rangeResponse);
103-
});
104-
});
107+
const stream = client.zscanStream('vehicles');
105108

106-
```
109+
stream.on('data', items => {
110+
// items = array of value, score, value, score...
111+
for (let n = 0; n < items.length; n += 2) {
112+
console.log(`${items[n]} -> ${items[n+1]}`);
113+
}
114+
});
107115

108-
Find more information about Redis & node-redis connections in the "[Redis Connect](https://github.com/redis-developer/redis-connect/tree/master/node.js/node-redis)" repository.
116+
stream.on('end', async () => {
117+
await client.quit();
118+
});
119+
} catch (e) {
120+
console.error(e);
121+
}
122+
}
109123

124+
ioredisDemo();
125+
```
110126

111127
</TabItem>
112128
</Tabs>

0 commit comments

Comments
 (0)