0Pricing
WebSockets & Real-Time Systems with Spring · บทเรียน

การติดตามสถานะการมีอยู่และสถานะออนไลน์

สร้างระบบสถานะแบบเรียลไทม์ที่แสดงว่าใครออนไลน์ พร้อมกระจายเหตุการณ์เชื่อมต่อและยกเลิกการเชื่อมต่อ และแสดงสถานะผู้ใช้แบบสด

การติดตามสถานะการมีอยู่และสถานะออนไลน์ เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

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

What Is Presence?

Presence is knowing who is currently online: the green dots in chat apps, the live cursors in collaborative editors, the active-now lists. It is one of the most requested real-time features.

Presence Is About Lifecycle Events

The core insight: presence is derived from connection lifecycle. When a session opens, mark the user online; when it closes, mark them offline, then broadcast the change.

Listening to Connect Events

Spring publishes a SessionConnectedEvent when a STOMP session is established. Use it to record the user as online.

@EventListener
public void onConnect(SessionConnectedEvent e) {
  String user = e.getUser().getName();
  presence.markOnline(user);
  broadcast(user, "ONLINE");
}

Listening to Disconnect Events

A matching SessionDisconnectEvent fires on close. Mark the user offline and broadcast it.

@EventListener
public void onDisconnect(SessionDisconnectEvent e) {
  String user = userFor(e.getSessionId());
  presence.markOffline(user);
  broadcast(user, "OFFLINE");
}

Storing Presence State

For a single server a concurrent map works. For a cluster, store presence in Redis so every node sees the same online set.

redis.opsForSet().add("online:users", username);
redis.opsForValue().set("presence:" + username, "ONLINE", Duration.ofMinutes(2));

Broadcasting Status Changes

Push each change to a presence topic so every interested client updates its UI live.

messagingTemplate.convertAndSend("/topic/presence",
  new PresenceEvent(username, status));

Multiple Tabs and Devices

A user with two tabs has two sessions. Reference-count them: only mark offline when the last session closes, or you will flicker the status.

Handling Ungraceful Disconnects

A crash may not send a clean close. Pair presence with heartbeats and a TTL: if no heartbeat arrives before the key expires, treat the user as offline.

Last-Seen Timestamps

Beyond online/offline, record a last-seen time on disconnect. The UI can then show "active 5 minutes ago" for offline users.

redis.opsForValue().set("lastseen:" + username, Instant.now().toString());

Sending the Initial Snapshot

When a client first connects it needs the current online list, not just future changes. Send a snapshot on subscription, then stream deltas.

@SubscribeMapping("/presence.init")
public Set<String> initial() {
  return presence.onlineUsers();
}

Privacy Considerations

Presence can leak behavior patterns. Offer an invisible mode and respect it server-side, so a user who opts out is never broadcast as online.

Quick Check

Test your presence knowledge.

Recap

You built presence:

  • Presence derives from connect/disconnect lifecycle events
  • Use SessionConnectedEvent / SessionDisconnectEvent and broadcast changes
  • Store shared state in Redis for clustering; reference-count multiple sessions
  • Pair with heartbeats and TTLs to catch ungraceful disconnects
  • Send an initial snapshot, track last-seen, and honor invisible mode

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

บทเรียน “การติดตามสถานะการมีอยู่และสถานะออนไลน์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การติดตามสถานะการมีอยู่และสถานะออนไลน์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การติดตามสถานะการมีอยู่และสถานะออนไลน์”

สร้างระบบสถานะแบบเรียลไทม์ที่แสดงว่าใครออนไลน์ พร้อมกระจายเหตุการณ์เชื่อมต่อและยกเลิกการเชื่อมต่อ และแสดงสถานะผู้ใช้แบบสด คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การติดตามสถานะการมีอยู่และสถานะออนไลน์” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม

ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. เหตุการณ์ที่ส่งจากเซิร์ฟเวอร์ (SSE) เทียบกับ WebSockets
  2. สถาปัตยกรรมการผลักข้อมูลแบบเรียลไทม์
  3. การนำการแจ้งเตือนผู้ใช้ไปใช้
  4. การติดตามสถานะการมีอยู่และสถานะออนไลน์
← กลับไปที่ WebSockets & Real-Time Systems with Spring