Skip to content

Commit 7171ee9

Browse files
committed
Refactored the documentation.
1 parent 62dd654 commit 7171ee9

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

README.md

+12-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Redis Keyspace Notifications with Node-Redis
22

3-
This is a small project using Node.js ([Node-Redis](https://www.npmjs.com/package/redis) client), [Redis](https://redis.io) and Redis [Keyspace Notifications](https://redis.io/docs/manual/keyspace-notifications/). Keyspace notifications allow clients to subscribe to [Pub/Sub](https://redis.io/docs/manual/pubsub/) channels in order to receive events affecting the Redis data set in some way. In this project, we'll use them to model a high score table in a multi-player game.
3+
This is a small project using [Redis](https://redis.io) and Redis [Keyspace Notifications](https://redis.io/docs/manual/keyspace-notifications/). Keyspace notifications allow clients to subscribe to [Pub/Sub](https://redis.io/docs/manual/pubsub/) channels in order to receive events affecting the Redis data set in some way. In this project, we'll use them to model a high score table in a multi-player game.
44

55
Imagine we're building a game in which players have to collect tokens, and where we measure who is winning by keeping track of how many tokens each player has. We'll model this in Redis as follows:
66

@@ -13,17 +13,19 @@ Here's what this project looks like when running... in the left hand terminal I'
1313

1414
![demo](keyspace_events_demo.gif)
1515

16+
1617
## Prerequisites
1718

1819
To try out this project yourself, you'll need:
1920

2021
* [Redis](https://redis.io) (install locally or use the supplied Docker Compose file).
2122
* [Docker Desktop](https://www.docker.com/products/docker-desktop/) (if using the Docker Compose file).
2223
* [git](https://git-scm.com/download) command line tools.
23-
* [Node.js](https://nodejs.org/) version 14.8 or higher.
24+
* [Node.js](https://nodejs.org/) version 14.8+ or [Python](https://www.python.org/) version 3.6+.
2425

2526
If you're not familiar with how pub/sub works in Redis, you may want to read the [guide on redis.io](https://redis.io/docs/manual/pubsub/).
2627

28+
2729
## Get the Code
2830

2931
To get the code, clone the repo to your machine:
@@ -33,6 +35,7 @@ $ git clone https://github.com/redis-developer/keyspace-notifications-node-redis
3335
$ cd keyspace-notifications-node-redis
3436
```
3537

38+
3639
## Configuring and Starting Redis
3740

3841
For performance reasons, keyspace notifications are off by default. If you're using the supplied Docker Compose file, this will start a Redis Stack container and pass it [extra configuration settings](https://redis.io/docs/stack/get-started/install/docker/#environment-variables) that enable a subset of keyspace notifications that we need for our application. If you go this route, there's nothing to do here - just start Redis Stack:
@@ -78,31 +81,13 @@ $ redis-cli
7881

7982
See the [Keyspace Notifications configuration docs](https://redis.io/docs/manual/keyspace-notifications/#configuration) for more information on configuring events.
8083

81-
## Application Setup
82-
83-
To setup the application, first install the dependencies:
84-
85-
```bash
86-
$ npm install
87-
```
88-
89-
The application expects to find Redis at `localhost` on port `6379` with no password. These are the default values when installing Redis.
9084

91-
If your Redis server is located elsewhere and/or requires a password, set the value of the `REDIS_URL` environment variable to a valid [Redis connection URL](https://github.com/redis/node-redis#usage) before starting the application. For example:
92-
93-
```bash
94-
$ export REDIS_URL=redis://simon:sssssh@redis.mydomain.com:6390
95-
```
96-
97-
## Running the Application
98-
99-
Start the application as follows:
85+
## Application Setup
10086

101-
```bash
102-
$ npm run dev
103-
```
87+
Check out the language-specific instructions:
88+
- [NodeJS](nodejs/README.md)
89+
- [Python](python/README.md)
10490

105-
This uses [nodemon](https://www.npmjs.com/package/nodemon) which will restart the application for you automatically every time you make a code change.
10691

10792
## Generating Events
10893

@@ -171,9 +156,11 @@ Scores:
171156

172157
## How does it Work?
173158

159+
> We're gonna be using the Node.js implementation to describe how the application works. The same logic applies to all the examples, regardless of the language of choice.
160+
174161
All of the code for this application is contained in a single file: `index.js`.
175162

176-
The keyspace notification events are generated by Redis itself, so there's no code required to create these. Instead our application needs to listen for them and take appropriate action.
163+
The keyspace notification events are generated by Redis itself, so there's no code required to create these. Instead our application needs to listen for them and take appropriate action.
177164

178165
The [`PSUBSCRIBE`](https://redis.io/commands/psubscribe/) command is one of the ways that we can listen for pub/sub events. The `P` in `PSUBSCRIBE` stands for "Pattern". Redis publishes pub/sub events to named channels. This command listens for events on pub/sub channels that match a glob style pattern.
179166

nodejs/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Application Setup
2+
3+
To setup the application, first install the dependencies:
4+
5+
```bash
6+
$ cd nodejs
7+
$ npm install
8+
```
9+
10+
The application expects to find Redis at `localhost` on port `6379` with no password. These are the default values when installing Redis.
11+
12+
If your Redis server is located elsewhere and/or requires a password, set the value of the `REDIS_URL` environment variable to a valid [Redis connection URL](https://github.com/redis/node-redis#usage) before starting the application. For example:
13+
14+
```bash
15+
$ export REDIS_URL=redis://simon:sssssh@redis.mydomain.com:6390
16+
```
17+
18+
## Running the Application
19+
20+
Start the application as follows:
21+
22+
```bash
23+
$ npm run dev
24+
```
25+
26+
This uses [nodemon](https://www.npmjs.com/package/nodemon) which will restart the application for you automatically every time you make a code change.

python/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Application Setup
2+
3+
To setup the application, first install the dependencies:
4+
5+
```bash
6+
$ cd python
7+
$ pip install -r requirements.txt
8+
```
9+
10+
The application expects to find Redis at `localhost` on port `6379` with no password. These are the default values when installing Redis.
11+
12+
If your Redis server is located elsewhere and/or requires a password, set the value of the `REDIS_URL` environment variable to a valid [Redis connection URL](https://redis.readthedocs.io/en/stable/examples/connection_examples.html#Connecting-to-Redis-instances-by-specifying-a-URL-scheme.) before starting the application. For example:
13+
14+
```bash
15+
$ export REDIS_URL=redis://luca:sssssh@redis.mydomain.com:6390
16+
```
17+
18+
Finally, the default logging level is set to `INFO`. You can change it with the `LOGGING_LEVEL` environment variable. For example:
19+
```bash
20+
$ export LOGGING_LEVEL=DEBUG
21+
```
22+
23+
## Running the Application
24+
25+
Start the application as follows:
26+
27+
```bash
28+
$ python main.py
29+
```

0 commit comments

Comments
 (0)