0Pricing
WebSockets & Real-Time Systems with Spring · درس

نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong

اكتشف اتصالات WebSocket المتوقفة مبكرًا باستخدام إطارات ping/pong ونبضات STOMP، ليحرر الخادم الموارد ويعيد العملاء الاتصال سريعًا.

نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong درس مجاني في 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 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Silent Death Problem

A WebSocket can look open long after the peer has vanished. A laptop lid closes, a network drops, a NAT mapping expires — yet neither side gets a close frame. This is a half-open connection.

Why It Matters

Half-open sockets waste server memory, keep sessions in maps forever, and let the client believe it is still receiving updates. You need an active probe to discover the truth: a heartbeat.

Ping/Pong Control Frames

The WebSocket protocol defines ping and pong control frames. One side sends a ping; a healthy peer must reply with a pong. No pong within a deadline means the connection is dead.

STOMP Heartbeats in Spring

When you use STOMP over WebSocket, Spring exposes heartbeat configuration. The two numbers are the rate (ms) the server sends and the rate it expects to receive heartbeats.

registry.enableSimpleBroker("/topic")
  .setHeartbeatValue(new long[]{10000, 10000})
  .setTaskScheduler(heartbeatScheduler());

A Scheduler Is Required

Heartbeats need a TaskScheduler to fire on time. Without one Spring silently disables them.

@Bean
public ThreadPoolTaskScheduler heartbeatScheduler() {
  ThreadPoolTaskScheduler s = new ThreadPoolTaskScheduler();
  s.setPoolSize(1);
  s.initialize();
  return s;
}

Client-Side STOMP Heartbeats

The client declares the matching heartbeat interval. Negotiation picks the slower of each side's value.

const client = new StompJs.Client({
  brokerURL: 'wss://api.example.com/ws',
  heartbeatIncoming: 10000,
  heartbeatOutgoing: 10000
});

Raw Ping at the Container Level

For plain (non-STOMP) handlers you can send ping frames directly from the server session.

PingMessage ping = new PingMessage(ByteBuffer.wrap(new byte[]{1}));
session.sendMessage(ping);

Choosing an Interval

Tune the interval to your needs:

  • Too short wastes bandwidth and battery on mobile
  • Too long delays detection of dead peers
  • 10–30 seconds is a common sweet spot

Beating Idle Timeouts

Proxies and load balancers drop connections that are idle too long (often 60s). A heartbeat doubles as keep-alive traffic that resets those idle timers and stops them from killing a healthy socket.

Reacting to a Missed Beat

When a heartbeat is missed, Spring closes the session and fires afterConnectionClosed. Use that hook to clean up state and let the client trigger its reconnect logic.

@Override
public void afterConnectionClosed(WebSocketSession s, CloseStatus status) {
  registry.remove(s.getId());
  log.warn("Closed {} - {}", s.getId(), status);
}

Heartbeats vs Application Acks

A heartbeat proves the transport is alive; it does not prove your message was processed. For business-level guarantees you still need application acknowledgements on top of heartbeats.

Quick Check

Test your grasp of keep-alives.

Recap

You made connections self-healing:

  • Half-open sockets die silently; heartbeats expose them
  • Ping/pong control frames probe transport health
  • STOMP heartbeats need a TaskScheduler and matching client intervals
  • Heartbeats also defeat proxy idle timeouts
  • Use the close callback to clean up and trigger reconnects

الأسئلة الشائعة

هل درس «نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong» مجاني؟

نعم — نص درس «نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Real-Time Systems with Spring، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Real-Time Systems with Spring 4 دروس في المجموع.

ماذا ستتعلم في «نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong»؟

اكتشف اتصالات WebSocket المتوقفة مبكرًا باستخدام إطارات ping/pong ونبضات STOMP، ليحرر الخادم الموارد ويعيد العملاء الاتصال سريعًا. تتمرن على WebSockets & Real-Time Systems with Spring مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ WebSockets & Real-Time Systems with Spring؟

لا تُشترط خبرة سابقة. WebSockets & Real-Time Systems with Spring على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس WebSockets & Real-Time Systems with Spring هذا؟

نعم. كل درس في WebSockets & Real-Time Systems with Spring يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التعامل السلس مع أخطاء WebSocket
  2. إدارة دورة حياة الاتصال
  3. إعادة المحاولة والبدائل
  4. نبضات القلب وإبقاء الاتصال حيًا باستخدام Ping/Pong
← العودة إلى WebSockets & Real-Time Systems with Spring