Redis Caching & Messaging (Pub/Sub, Streams) · レッスン

プレゼンスとオンライン状態の追跡

Redisのセット、TTL、Pub/Subのハートビートを使ってオンライン中のユーザーを追跡するリアルタイムプレゼンスシステムを構築します。

レッスン 4/413 ステップ

「プレゼンスとオンライン状態の追跡」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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) — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「プレゼンスとオンライン状態の追跡」レッスンは無料ですか?

はい。「プレゼンスとオンライン状態の追跡」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、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)を演習し、24時間対応の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)に戻る