Conversation
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
petyaslavova
left a comment
There was a problem hiding this comment.
Hey @r266-tech, thank you for your contribution!
| "import redis\n", | ||
| "\n", | ||
| "# Subscriber connection\n", | ||
| "sub_client = redis.Redis(decode_responses=True)\n", |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
You can also use the setting to ignore the confirmation messages
There was a problem hiding this comment.
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.
Summary
Adds a Jupyter notebook covering Redis Pub/Sub patterns, addressing #1744.
What's included
psubscribewith wildcardsget_message()with timeoutWhy 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.ipynbJupyter 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.rstto include the newexamples/pubsub_examplesentry in the documentation toctree.Written by Cursor Bugbot for commit e92a97c. This will update automatically on new commits. Configure here.