Skip to content

Commit d0bfa77

Browse files
authored
#2287 Add example scripts showing pub/sub usage. (#2288)
* #2287 Add example scripts showing pub/sub usage. #2287 * #2287 Add example scripts showing pub/sub usage. Fixing comments requested Adding client.connect() to pubsub-subscriber.js Reformatting Readme updating logging in pubsub-publisher.js * #2287 Add example scripts showing pub/sub usage. Fix publish and subscriber Update tidy up comments * Update examples/pubsub-subscriber.js Making suggested changes Co-authored-by: Simon Prickett <[email protected]> Co-authored-by: Simon Prickett <[email protected]> Closes #2287.
1 parent 9398e5d commit d0bfa77

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This folder contains example scripts showing how to use Node Redis in different
1414
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog) |
1515
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
1616
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
17+
| `pubsub-publisher.js` | Adds multiple messages on 2 different channels messages to Redis |
18+
| `pubsub-subscriber.js` | Reads messages from channels using `PSUBSCRIBE` command |
1719
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
1820
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
1921
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |

examples/pubsub-publisher.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// A sample publisher using the publish function to put message on different channels.
2+
// https://redis.io/commands/publish/
3+
import { createClient } from 'redis';
4+
5+
const client = createClient();
6+
7+
await client.connect();
8+
9+
// Declare constant variables for the name of the clients we will publish to as they will be required for logging.
10+
const channel1 = 'chan1nel';
11+
const channel2 = 'chan2nel';
12+
13+
for (let i = 0; i < 10000; i++) {
14+
// 1st channel created to publish 10000 messages.
15+
await client.publish(channel1, `channel1_message_${i}`);
16+
console.log(`publishing message on ${channel1}`);
17+
// 2nd channel created to publish 10000 messages.
18+
await client.publish(channel2, `channel2_message_${i}`);
19+
console.log(`publishing message on ${channel2}`);
20+
}

examples/pubsub-subscriber.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// A sample subscriber showing how the subscribe method and pSubscribe method work.
2+
// https://redis.io/commands/subscribe/
3+
// https://redis.io/commands/pSubscribe/
4+
// This consumes messages published by pubsub-publisher.js
5+
6+
import { createClient} from 'redis';
7+
8+
// Create and connect client before executing any Redis commands.
9+
const client = createClient();
10+
await client.connect();
11+
12+
// Each subscriber needs to connect individually therefore we duplicate the client.
13+
const channel1Sub = client.duplicate();
14+
const channel2Sub = client.duplicate();
15+
const noChannelsSub = client.duplicate();
16+
const allChannelsSub = client.duplicate();
17+
18+
await channel1Sub.connect();
19+
await channel2Sub.connect();
20+
await noChannelsSub.connect();
21+
await allChannelsSub.connect();
22+
23+
// This subscriber only will receive messages from channel 1 as they are using the subscribe method and subscribed to chan1nel.
24+
await channel1Sub.subscribe('chan1nel', (message) => {
25+
console.log(`Channel1 subscriber collected message: ${message}`);
26+
},true);
27+
28+
// This subscriber only will receive messages from channel 2 as they are using the subscribe method and subscribed to chan2nel.
29+
await channel2Sub.subscribe('chan2nel', (message) => {
30+
console.log(`Channel2 subscriber collected message: ${message}`);
31+
},true);
32+
33+
// This subscriber will not receive any messages as its channel does not exist.
34+
await noChannelsSub.subscribe('chan*nel', (message) => {
35+
console.log(`This message will never be seen as we are not using pSubscribe here. ${message}`);
36+
},true);
37+
38+
// This subscriber receive messages from both channel 1 and channel 2 using the pSubscribe method.
39+
await allChannelsSub.pSubscribe('chan*nel', (message, channel) => {
40+
console.log(`Channel ${channel} sent message: ${message}`);
41+
},true);

0 commit comments

Comments
 (0)