close
Skip to content

Add Pub/Sub examples notebook#4007

Open
r266-tech wants to merge 1 commit intoredis:masterfrom
r266-tech:add-pubsub-examples
Open

Add Pub/Sub examples notebook#4007
r266-tech wants to merge 1 commit intoredis:masterfrom
r266-tech:add-pubsub-examples

Conversation

@r266-tech
Copy link
Copy Markdown

@r266-tech r266-tech commented Mar 23, 2026

Summary

Adds a Jupyter notebook covering Redis Pub/Sub patterns, addressing #1744.

What's included

  • Basic Pub/Sub: subscribe, publish, receive messages
  • Pattern-based subscriptions: using psubscribe with wildcards
  • Listening in a loop: practical get_message() with timeout
  • Unsubscribing and cleanup: resource management best practices
  • JSON messages: structured communication pattern

Why Pub/Sub?

Pub/Sub is a core Redis feature widely used for real-time notifications, event-driven architectures, and decoupled microservices. It was missing from the examples directory.

Relates to #1744


Note

Low Risk
Documentation-only change that adds a new example notebook and links it from the examples index; no runtime/library code paths are modified.

Overview
Adds a new docs/examples/pubsub_examples.ipynb Jupyter notebook demonstrating Redis Pub/Sub usage patterns (basic subscribe/publish, pattern subscriptions, looped consumption with timeout, cleanup/unsubscribe, and JSON-encoded payloads).

Updates docs/examples.rst to include the new examples/pubsub_examples entry in the documentation toctree.

Written by Cursor Bugbot for commit e92a97c. This will update automatically on new commits. Configure here.

Adds a Jupyter notebook covering Redis Pub/Sub patterns:
- Basic publish/subscribe
- Pattern-based subscriptions with psubscribe
- Listening in a loop with get_message
- Unsubscribing and cleanup
- Sending JSON-encoded messages

Relates to redis#1744
Copy link
Copy Markdown
Collaborator

@petyaslavova petyaslavova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @r266-tech, thank you for your contribution!

"import redis\n",
"\n",
"# Subscriber connection\n",
"sub_client = redis.Redis(decode_responses=True)\n",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename the instance to redis_client

"outputs": [],
"source": [
"# Publisher connection (must be separate from subscriber)\n",
"pub_client = redis.Redis(decode_responses=True)\n",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the same client instance. To subscribe you create a pubsub instance, and to publish, you just use the instance to call the publish command.

"# Read all pending messages with a timeout\n",
"while True:\n",
" message = pubsub_loop.get_message(timeout=1.0)\n",
" if message is None:\n",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you exit the cycle on None?

"\n",
"pubsub_json = sub_client.pubsub()\n",
"pubsub_json.subscribe(\"task_updates\")\n",
"pubsub_json.get_message() # consume confirmation"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use the setting to ignore the confirmation messages

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more interesting case is to subscribe from one pubsub to concrete name as well as to some pattern - if you have somme time, I believve it will alaso be a valuable addition to this example notebook.

@petyaslavova petyaslavova added changes-requested maintenance Maintenance (CI, Releases, etc) labels Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes-requested maintenance Maintenance (CI, Releases, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants