Skip to content

Commit

Permalink
#2287 Add example scripts showing pub/sub usage. (#2288)
Browse files Browse the repository at this point in the history
* #2287 Add example scripts showing pub/sub usage.
redis/node-redis#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 <simon@crudworks.org>

Co-authored-by: Simon Prickett <simon@crudworks.org>

Closes #2287.
  • Loading branch information
supercoder1213 committed Oct 14, 2022
1 parent 36ea2a6 commit e5ef267
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/README.md
Expand Up @@ -14,6 +14,8 @@ This folder contains example scripts showing how to use Node Redis in different
| `hyperloglog.js` | Showing use of Hyperloglog commands [PFADD, PFCOUNT and PFMERGE](https://redis.io/commands/?group=hyperloglog) |
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
| `pubsub-publisher.js` | Adds multiple messages on 2 different channels messages to Redis |
| `pubsub-subscriber.js` | Reads messages from channels using `PSUBSCRIBE` command |
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
Expand Down
20 changes: 20 additions & 0 deletions examples/pubsub-publisher.js
@@ -0,0 +1,20 @@
// A sample publisher using the publish function to put message on different channels.
// https://redis.io/commands/publish/
import { createClient } from 'redis';

const client = createClient();

await client.connect();

// Declare constant variables for the name of the clients we will publish to as they will be required for logging.
const channel1 = 'chan1nel';
const channel2 = 'chan2nel';

for (let i = 0; i < 10000; i++) {
// 1st channel created to publish 10000 messages.
await client.publish(channel1, `channel1_message_${i}`);
console.log(`publishing message on ${channel1}`);
// 2nd channel created to publish 10000 messages.
await client.publish(channel2, `channel2_message_${i}`);
console.log(`publishing message on ${channel2}`);
}
41 changes: 41 additions & 0 deletions examples/pubsub-subscriber.js
@@ -0,0 +1,41 @@
// A sample subscriber showing how the subscribe method and pSubscribe method work.
// https://redis.io/commands/subscribe/
// https://redis.io/commands/pSubscribe/
// This consumes messages published by pubsub-publisher.js

import { createClient} from 'redis';

// Create and connect client before executing any Redis commands.
const client = createClient();
await client.connect();

// Each subscriber needs to connect individually therefore we duplicate the client.
const channel1Sub = client.duplicate();
const channel2Sub = client.duplicate();
const noChannelsSub = client.duplicate();
const allChannelsSub = client.duplicate();

await channel1Sub.connect();
await channel2Sub.connect();
await noChannelsSub.connect();
await allChannelsSub.connect();

// This subscriber only will receive messages from channel 1 as they are using the subscribe method and subscribed to chan1nel.
await channel1Sub.subscribe('chan1nel', (message) => {
console.log(`Channel1 subscriber collected message: ${message}`);
},true);

// This subscriber only will receive messages from channel 2 as they are using the subscribe method and subscribed to chan2nel.
await channel2Sub.subscribe('chan2nel', (message) => {
console.log(`Channel2 subscriber collected message: ${message}`);
},true);

// This subscriber will not receive any messages as its channel does not exist.
await noChannelsSub.subscribe('chan*nel', (message) => {
console.log(`This message will never be seen as we are not using pSubscribe here. ${message}`);
},true);

// This subscriber receive messages from both channel 1 and channel 2 using the pSubscribe method.
await allChannelsSub.pSubscribe('chan*nel', (message, channel) => {
console.log(`Channel ${channel} sent message: ${message}`);
},true);

0 comments on commit e5ef267

Please sign in to comment.