Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Redis and Node.js #630

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: index-redisandnodejs
title: Using Redis from Node.js
title: Using Redis with Node.js
sidebar_label: Redis and Node.js
slug: /develop/node/nodecrashcourse/redisandnodejs
authors: [simon]
Expand All @@ -10,51 +10,36 @@ import Authors from '@theme/Authors';

<Authors frontMatter={frontMatter} />

<div class="text--center">
<iframe
width="896"
height="509"
src="https://www.youtube.com/embed/vXkYOSpKFHc"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
</div>

To connect to Redis from an application, we need a Redis client library for the language that we're coding in. Redis clients perform the following functions:

- Manage the connections between our application and the Redis server.
- Handle network communications to the Redis server using Redis' wire protocol.
- Provide a language specific API that we use in our application.
- Provide a language-specific API that we use in our application.

For Node.js, there are two popular Redis clients: ioredis and node_redis. Both clients expose similar programming APIs, wrapping each Redis command as a function that we can call in a Node.js script. For this course, we'll use ioredis which has built in support for modern JavaScript features such as Promises.
For Node.js, there are two popular Redis clients: [node-redis](https://www.npmjs.com/package/redis) and [ioredis](https://www.npmjs.com/package/ioredis). Both clients expose similar programming APIs, wrapping each Redis command as a function that we can call in a Node.js script. For this course, we'll use node-redis.

Here's a complete Node.js script that uses ioredis to perform the SET and GET commands that we previously tried in redis-cli:
Here's a complete Node.js script that uses node-redis to perform the SET and GET commands that we previously tried in redis-cli:

```javascript
const Redis = require('ioredis');

const redisDemo = async () => {
// Connect to Redis at 127.0.0.1, port 6379.
const redisClient = new Redis({
host: '127.0.0.1',
port: 6379,
});
import { createClient } from 'redis';

// Set key "myname" to have value "Simon Prickett".
await redisClient.set('myname', 'Simon Prickett');
// Connect to Redis at 127.0.0.1, port 6379.
const redisClient = await createClient({ url: 'redis://127.0.0.1:6379' })
.on('error', err => console.error('Redis Client Error', err))
.connect();

// Get the value held at key "myname" and log it.
const value = await redisClient.get('myname');
console.log(value);
// Set key "myname" to have value "Simon Prickett".
await redisClient.set('myname', 'Simon Prickett');

// Disconnect from Redis.
redisClient.quit();
};
// Get the value held at key "myname" and log it.
const value = await redisClient.get('myname');
console.log(value);

redisDemo();
// Disconnect from Redis.
await redisClient.quit();
```

ioredis wraps each Redis command in a function that can either accept a callback or return a Promise. Here, I'm using async/await to wait for each command to be executed on the Redis server before moving on to the next.
node-redis wraps each Redis command in a function that returns a `Promise`. Here, I'm using async/await to wait for each command to be executed on the Redis server before moving on to the next.

Running this code displays the value that's now stored in Redis:

Expand All @@ -67,19 +52,7 @@ Simon Prickett

The following additional resources can help you understand how to access Redis from a Node.js application:

- [ioredis](https://github.com/luin/ioredis): Home page for the ioredis client.
- [node_redis](https://redis.js.org/): Home page for the node_redis client.
- [node-redis](https://www.npmjs.com/package/redis): node-redis on npm.
- [ioredis](https://www.npmjs.com/package/ioredis): ioredis on npm.
- [RU102JS, Redis for JavaScript Developers](https://university.redis.com/courses/ru102js/): A free online course at Redis University that provides a deep dive into Redis for Node.js applications. You can expect to learn how to make connections to Redis, store and retrieve data, and leverage essential Redis features such as sorted sets and streams.
- [Redis clients by programming language](https://redis.io/clients): A large list of Redis clients at redis.io.

In this video, I take a look at how to get up and running with the ioredis client:

<div class="text--center">
<iframe
width="896"
height="509"
src="https://www.youtube.com/embed/H6rikGCYPUk"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
</div>