تتبع الحضور وحالة الاتصال
ابنِ نظام حضور لحظيًا يعرض المتصلين، ويبث أحداث الاتصال وقطع الاتصال، ويُظهر حالة المستخدمين المباشرة.
تتبع الحضور وحالة الاتصال درس مجاني في WebSockets & Real-Time Systems with Spring على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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/SessionDisconnectEventand 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
الأسئلة الشائعة
هل درس «تتبع الحضور وحالة الاتصال» مجاني؟
نعم — نص درس «تتبع الحضور وحالة الاتصال» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Real-Time Systems with Spring، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Real-Time Systems with Spring 4 دروس في المجموع.
ماذا ستتعلم في «تتبع الحضور وحالة الاتصال»؟
ابنِ نظام حضور لحظيًا يعرض المتصلين، ويبث أحداث الاتصال وقطع الاتصال، ويُظهر حالة المستخدمين المباشرة. تتمرن على WebSockets & Real-Time Systems with Spring مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الأحداث المرسلة من الخادم (SSE) مقابل WebSockets
- بنيات دفع البيانات الفورية
- تنفيذ إشعارات المستخدمين
- تتبع الحضور وحالة الاتصال