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

ตัวบ่งชี้การพิมพ์และสถานะออนไลน์

เรียนรู้การเพิ่มตัวบ่งชี้การพิมพ์แบบเรียลไทม์และสถานะออนไลน์ให้แอปแชตด้วยเหตุการณ์ STOMP สัญญาณชีพ และการติดตามสถานะผู้ใช้ใน 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 บทเรียน

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

Beyond Messages

Great chat apps do more than deliver messages. Typing indicators and online presence make conversations feel live and responsive.

This lesson adds both features on top of your existing STOMP chat.

What Is Presence?

Presence tells users who is currently online, away, or offline. It is updated in real time as users connect and disconnect, giving the social cue that someone is available to chat.

Tracking Connections

Spring publishes SessionConnectedEvent and SessionDisconnectEvent. Listen for them to maintain a live set of online users.

@EventListener
public void onConnect(SessionConnectedEvent event) {
    String user = userFrom(event);
    onlineUsers.add(user);
    broadcastPresence(user, 'ONLINE');
}

Handling Disconnects

When a user disconnects, remove them and notify others so the presence list stays accurate.

@EventListener
public void onDisconnect(SessionDisconnectEvent event) {
    String user = userFrom(event);
    onlineUsers.remove(user);
    broadcastPresence(user, 'OFFLINE');
}

Broadcasting Presence

Send presence updates to a shared topic so every connected client can update its UI.

private void broadcastPresence(String user, String status) {
    Map<String, String> event = new HashMap<>();
    event.put('user', user);
    event.put('status', status);
    messagingTemplate.convertAndSend('/topic/presence', event);
}

A Typing Event Model

Typing indicators are lightweight events, not stored messages. Define a small payload carrying the sender, the room, and whether they started or stopped typing.

{
  "type": "TYPING",
  "room": "general",
  "sender": "alice",
  "typing": true
}

Receiving Typing Events

Map a destination for typing events and relay them to the room topic without persisting them.

@MessageMapping('/chat.typing')
public void typing(TypingEvent event) {
    messagingTemplate.convertAndSend(
        '/topic/room.' + event.getRoom() + '.typing', event);
}

Debouncing on the Client

Clients should not send a typing event for every keystroke. Debounce: send 'typing' once, then send 'stopped' after a short idle period. This reduces traffic dramatically.

let timer = null;
function onKeyPress() {
  sendTyping(true);
  clearTimeout(timer);
  timer = setTimeout(function () { sendTyping(false); }, 2000);
}

Heartbeats for Stale Sessions

Network drops do not always fire a disconnect event. Use STOMP heartbeats so the server can detect dead connections and mark those users offline, keeping presence accurate.

Scaling Presence

With multiple server instances, presence must be shared. Store online users in a central store like Redis and broadcast changes across instances so every client sees a consistent presence list.

Privacy Considerations

Presence and typing reveal user activity. Let users control visibility, and only share presence with people who should see it (for example, contacts or members of the same room), not everyone on the server.

Quick Check

Test your understanding of presence and typing indicators.

Recap

You learned to add online presence using Spring's session connect and disconnect events and typing indicators as lightweight, non-persisted STOMP events. Debouncing, heartbeats, a shared store for scaling, and privacy controls make these features production-ready.

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

บทเรียน “ตัวบ่งชี้การพิมพ์และสถานะออนไลน์” ฟรีหรือไม่

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

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

เรียนรู้การเพิ่มตัวบ่งชี้การพิมพ์แบบเรียลไทม์และสถานะออนไลน์ให้แอปแชตด้วยเหตุการณ์ STOMP สัญญาณชีพ และการติดตามสถานะผู้ใช้ใน Spring คุณปฏิบัติ 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. การออกแบบแบบจำลองข้อความแชต
  2. การสร้างห้องแชตสาธารณะ
  3. การส่งข้อความส่วนตัวด้วย STOMP
  4. ตัวบ่งชี้การพิมพ์และสถานะออนไลน์
← กลับไปที่ WebSockets & Real-Time Systems with Spring