0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · บทเรียน

การกระจายข้อความอย่างง่าย

ฝึกเผยแพร่ข้อความไปยังช่องและรับข้อความด้วยการสมัครรับ โดยใช้ Redis CLI และไลบรารีไคลเอนต์

การกระจายข้อความอย่างง่าย เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Redis Caching & Messaging (Pub/Sub, Streams) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is Broadcasting?

In the world of Pub/Sub, broadcasting means sending a single message that can be received by many listeners at once.

Think of it like a radio station: the station broadcasts music, and anyone tuned to that frequency can hear it.

  • One sender (publisher)
  • Many receivers (subscribers)
  • Decoupled communication

The Publisher's Role

A publisher is the entity that sends messages. It doesn't care who is listening or if anyone is listening at all.

Publishers simply send messages to a specific channel. Redis then takes care of delivering that message to all active subscribers of that channel.

CLI: Publishing Messages

You can easily publish messages using the Redis Command-Line Interface (CLI). The command is straightforward:

PUBLISH channel_name "Your message content"

Let's say you want to send a news update to a channel called newsfeed:

PUBLISH newsfeed "Breaking: Redis is awesome!"

The Subscriber's Role

A subscriber is the entity that listens for messages. It 'tunes in' to one or more specific channels.

When a message is published to a channel a subscriber is listening to, Redis delivers that message to the subscriber.

CLI: Subscribing to Channels

To start listening for messages on a channel via the Redis CLI, you use the SUBSCRIBE command:

SUBSCRIBE channel_name

Once you execute this command, your CLI will enter a listening mode, waiting for messages.

SUBSCRIBE newsfeed

Demo: CLI Publish & Subscribe

To see Pub/Sub in action with the CLI, you need two separate terminal windows:

  1. Window 1 (Subscriber): Run redis-cli SUBSCRIBE chatroom
  2. Window 2 (Publisher): Run redis-cli PUBLISH chatroom "Hello everyone!"

You'll see the message appear instantly in Window 1!

# Window 1 (Subscriber)
redis-cli SUBSCRIBE chatroom

# Window 2 (Publisher)
redis-cli PUBLISH chatroom "Hello everyone!"

Beyond CLI: Client Libraries

While the CLI is great for testing, real applications use client libraries. These libraries provide APIs in your preferred programming language (like Python, Java, Node.js) to interact with Redis.

Using client libraries makes it easy to integrate Redis Pub/Sub into your application logic.

Python Client: Publishing

Here's how to publish a message using the popular redis-py client library in Python. This script will connect to Redis and send a message to a channel.

import redis

r = redis.Redis(decode_responses=True)

channel_name = "app_updates"
message_content = "New feature released! Check it out."

r.publish(channel_name, message_content)
print(f"Published '{message_content}' to '{channel_name}'")

Python Client: Subscribing

This Python script demonstrates how to subscribe to a channel and receive a single message. It then exits, making it suitable for a quick runnable example.

In a real application, the for message in pubsub.listen(): loop would typically run continuously.

import redis

r = redis.Redis(decode_responses=True)
pubsub = r.pubsub()

channel_name = "app_updates"
pubsub.subscribe(channel_name)

print(f"Listening on '{channel_name}' for one message...")
for message in pubsub.listen():
    if message['type'] == 'message':
        print(f"Received: {message['data']}")
        break # Exit after first message
print("Subscriber finished.")

Quick Check: Pub/Sub Actions

You've learned about the fundamental commands for Redis Pub/Sub. Let's see if you can identify the correct action.

Recap: Broadcasting Messages

In this lesson, we explored how to broadcast messages using Redis Pub/Sub:

  • Publishers send messages to channels without knowing the subscribers.
  • Subscribers listen to specific channels to receive messages.
  • We used the Redis CLI with PUBLISH and SUBSCRIBE.
  • We also saw how to implement simple publishers and subscribers using Python client libraries.

Next, we'll dive deeper into building more complex real-time applications!

คำถามที่พบบ่อย

บทเรียน “การกระจายข้อความอย่างง่าย” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การกระจายข้อความอย่างง่าย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การกระจายข้อความอย่างง่าย”

ฝึกเผยแพร่ข้อความไปยังช่องและรับข้อความด้วยการสมัครรับ โดยใช้ Redis CLI และไลบรารีไคลเอนต์ คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การกระจายข้อความอย่างง่าย” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม

ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่การเผยแพร่และรับสมาชิก
  2. กลไกการเผยแพร่และรับสมาชิกของ Redis
  3. การกระจายข้อความอย่างง่าย
  4. ช่องทางเทียบกับการแจ้งเตือนคีย์สเปซ
← กลับไปที่ Redis Caching & Messaging (Pub/Sub, Streams)