0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 课时

在线状态与在线用户跟踪

使用 Redis 集合、TTL 和发布/订阅心跳,构建实时在线状态系统来跟踪哪些用户在线

在线状态与在线用户跟踪 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 online

Heartbeats 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 seconds

Listing 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:7

Sorted 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:id on each heartbeat
  • ZREMRANGEBYSCORE online -inf <cutoff> removes timed-out users
ZADD online 1716900010 user:42
ZREMRANGEBYSCORE online -inf 1716899980

Broadcasting 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.events

Detecting 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__:expired

Per-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:online

Multi-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:42

Putting 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.

常见问题解答

「在线状态与在线用户跟踪」课时是免费的吗?

是的 — 「在线状态与在线用户跟踪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。

「在线状态与在线用户跟踪」这节课中我会学到什么?

使用 Redis 集合、TTL 和发布/订阅心跳,构建实时在线状态系统来跟踪哪些用户在线 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「在线状态与在线用户跟踪」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?

能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 按模式匹配订阅
  2. 设计实时聊天
  3. 事件驱动架构
  4. 在线状态与在线用户跟踪
← 返回 Redis Caching & Messaging (Pub/Sub, Streams)