-
Notifications
You must be signed in to change notification settings - Fork 1.9k
#2287 Add example scripts showing pub/sub usage. #2288
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d29d5bf
#2287 Add example scripts showing pub/sub usage.
con-mark f01acaf
#2287 Add example scripts showing pub/sub usage.
con-mark aa7541b
#2287 Add example scripts showing pub/sub usage.
con-mark 27bbbb4
Update examples/pubsub-subscriber.js
con-mark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not receive any messages cause 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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.