You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+12-25
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Redis Keyspace Notifications with Node-Redis
2
2
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.
4
4
5
5
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:
6
6
@@ -13,17 +13,19 @@ Here's what this project looks like when running... in the left hand terminal I'
13
13
14
14

15
15
16
+
16
17
## Prerequisites
17
18
18
19
To try out this project yourself, you'll need:
19
20
20
21
*[Redis](https://redis.io) (install locally or use the supplied Docker Compose file).
21
22
*[Docker Desktop](https://www.docker.com/products/docker-desktop/) (if using the Docker Compose file).
22
23
*[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+.
24
25
25
26
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/).
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
78
81
79
82
See the [Keyspace Notifications configuration docs](https://redis.io/docs/manual/keyspace-notifications/#configuration) for more information on configuring events.
80
83
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.
90
84
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:
This uses [nodemon](https://www.npmjs.com/package/nodemon) which will restart the application for you automatically every time you make a code change.
106
91
107
92
## Generating Events
108
93
@@ -171,9 +156,11 @@ Scores:
171
156
172
157
## How does it Work?
173
158
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
+
174
161
All of the code for this application is contained in a single file: `index.js`.
175
162
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.
177
164
178
165
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.
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:
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:
0 commit comments