การติดตามการมีอยู่และสถานะออนไลน์
สร้างระบบแสดงสถานะแบบเรียลไทม์ที่ติดตามว่าผู้ใช้คนใดออนไลน์อยู่ โดยใช้เซตของ Redis, TTL และสัญญาณชีพ Pub/Sub
การติดตามการมีอยู่และสถานะออนไลน์ เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Redis Caching & Messaging (Pub/Sub, Streams) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What Is Presence?
Presence is the real-time indication of whether a user is online, away, or offline. Chat apps, collaborative editors, and games all rely on it. Redis is ideal because presence data is ephemeral and high-churn.
The Naive Approach
A first idea: store a flag per user. The problem is detecting when someone disconnects ungracefully (closed laptop, lost network). A stale flag would mark them online forever.
SET presence:user:42 onlineHeartbeats with TTL
The robust pattern uses heartbeats. The client refreshes a key with a short TTL every few seconds. If heartbeats stop, the key expires and the user is considered offline.
SET presence:user:42 1 EX 15
# client re-issues this every 10 secondsListing Online Users
To list everyone online, keep a set of active users and check membership, or scan presence keys. A sorted set keyed by last-seen timestamp is efficient.
ZADD online 1716900000 user:42
ZADD online 1716900005 user:7Sorted Set by Timestamp
Storing last-seen as the score lets you both list members and prune the stale ones in one query.
ZADD online <now> user:idon each heartbeatZREMRANGEBYSCORE online -inf <cutoff>removes timed-out users
ZADD online 1716900010 user:42
ZREMRANGEBYSCORE online -inf 1716899980Broadcasting Status Changes
When a user comes online or drops off, publish an event so other clients update their UI immediately.
PUBLISH presence.events "user:42 online"
SUBSCRIBE presence.eventsDetecting Going Offline
Combine the sorted set prune with keyspace expiry. When a presence key expires, a keyevent fires, and a worker publishes a went-offline event to subscribers.
CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expiredPer-Room Presence
In a chat app you often need presence per room. Use one set per room and add or remove the user as they join or leave.
SADD room:general:online user:42
SREM room:general:online user:42
SMEMBERS room:general:onlineMulti-Device Presence
A user may be online from several devices. Count active connections so the user only goes offline when the last device disconnects.
INCR conns:user:42
DECR conns:user:42
GET conns:user:42Putting It Together
A complete presence system: heartbeat TTL keys, a last-seen sorted set, connection counters for multi-device, and Pub/Sub events to push changes to clients in real time.
Tuning the Intervals
Choose a heartbeat interval shorter than the TTL (for example heartbeat every 10s, TTL 15s) so a single missed beat does not flap a user offline. Shorter intervals mean faster detection but more traffic.
Quick Check
Test your understanding of presence design.
Recap
You built a real-time presence system using heartbeat TTL keys, a last-seen sorted set with score-based pruning, per-room presence sets, multi-device connection counters, and Pub/Sub broadcasts for instant status updates. Tune heartbeat and TTL intervals to balance detection speed against traffic.
เรียนรู้ Redis Caching & Messaging (Pub/Sub, Streams) ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การติดตามการมีอยู่และสถานะออนไลน์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การติดตามการมีอยู่และสถานะออนไลน์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การติดตามการมีอยู่และสถานะออนไลน์”
สร้างระบบแสดงสถานะแบบเรียลไทม์ที่ติดตามว่าผู้ใช้คนใดออนไลน์อยู่ โดยใช้เซตของ Redis, TTL และสัญญาณชีพ Pub/Sub คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การติดตามการมีอยู่และสถานะออนไลน์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม
ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสมัครรับตามรูปแบบ
- การออกแบบแชตแบบเรียลไทม์
- สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์
- การติดตามการมีอยู่และสถานะออนไลน์