مؤشرات الكتابة والحضور عبر الإنترنت
تعلّم إضافة مؤشرات الكتابة والحضور عبر الإنترنت في الوقت الفعلي إلى تطبيق محادثة باستخدام أحداث STOMP ونبضات القلب وتتبع الحضور في Spring.
مؤشرات الكتابة والحضور عبر الإنترنت درس مجاني في 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 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
الأسئلة الشائعة
هل درس «مؤشرات الكتابة والحضور عبر الإنترنت» مجاني؟
نعم — نص درس «مؤشرات الكتابة والحضور عبر الإنترنت» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تصميم نموذج رسائل الدردشة
- تنفيذ غرف الدردشة العامة
- المراسلة الخاصة باستخدام STOMP
- مؤشرات الكتابة والحضور عبر الإنترنت