0Pricing
GraphQL APIs with Spring Boot · درس

تصفية الاشتراكات وتوسيع نطاقها

تجاوز الاشتراكات الأساسية: أوصل إلى كل عميل الأحداث التي تهمه فقط باستخدام التصفية من جانب الخادم، ووسّع نطاق الاشتراكات عبر عدة مثيلات من Spring Boot.

تصفية الاشتراكات وتوسيع نطاقها درس مجاني في GraphQL APIs with Spring Boot على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في GraphQL APIs with Spring Boot، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.

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

Not Every Client Wants Everything

A naive subscription pushes every event to every subscriber. But a user watching order #42 does not care about order #99.

Filtering ensures each subscriber only receives the events relevant to them.

Subscription Arguments

Subscriptions can take arguments just like queries. A client passes the ID it cares about, and the server uses it to filter the stream.

type Subscription {
  orderUpdated(orderId: ID!): Order
}

Reactive Streams Recap

Spring for GraphQL represents a subscription as a Reactor Flux, an asynchronous stream of items. Filtering is just stream operations applied to that Flux.

Filtering with filter()

Apply .filter() to the publisher so only matching events flow to the subscriber.

@SubscriptionMapping
public Flux<Order> orderUpdated(@Argument String orderId) {
    return orderPublisher.flux()
        .filter(o -> o.getId().equals(orderId));
}

A Shared Event Sink

Use a Reactor Sinks.Many as a hub. Your service emits events into it, and every subscription builds a filtered view of its stream.

private final Sinks.Many<Order> sink =
    Sinks.many().multicast().onBackpressureBuffer();

Emitting Events

When your business logic changes an order, push it into the sink so all matching subscribers are notified.

public void updateOrder(Order order) {
    repository.save(order);
    sink.tryEmitNext(order);
}

The Scaling Problem

An in-memory sink only knows about events on its own JVM. With multiple Spring Boot instances behind a load balancer, an event emitted on instance A never reaches subscribers connected to instance B.

External Pub/Sub to the Rescue

Route events through an external broker like Redis Pub/Sub or Kafka. Every instance publishes to and subscribes from the broker, so all instances see all events.

Bridging Redis to a Flux

Subscribe to a Redis channel and feed incoming messages into the same Flux your GraphQL subscription exposes, unifying local and remote events.

redisTemplate.listenToChannel("orders")
    .map(msg -> deserialize(msg.getMessage()))
    .subscribe(sink::tryEmitNext);

Backpressure and Cleanup

Slow clients can fall behind. Use a bounded buffer or drop strategy, and ensure resources are released when a client disconnects so memory does not leak.

Sinks.many().multicast().onBackpressureBuffer(1024, false);

Best Practices

Keep subscriptions efficient:

  • Filter on the server, never push everything
  • Use an external broker to scale across instances
  • Handle backpressure for slow consumers
  • Clean up on disconnect to avoid leaks

Quick Check

Test your subscription scaling knowledge.

Recap

You scaled real-time subscriptions:

  • Filter streams with .filter() using subscription arguments
  • Use a Sinks.Many hub to broadcast events
  • Route through Redis or Kafka to scale across instances
  • Manage backpressure and clean up on disconnect

Filtered, distributed subscriptions deliver the right data at scale.

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

هل درس «تصفية الاشتراكات وتوسيع نطاقها» مجاني؟

نعم — نص درس «تصفية الاشتراكات وتوسيع نطاقها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة GraphQL APIs with Spring Boot، انتقل إلى CoddyKit PRO. تتضمن دورة GraphQL APIs with Spring Boot 4 دروس في المجموع.

ماذا ستتعلم في «تصفية الاشتراكات وتوسيع نطاقها»؟

تجاوز الاشتراكات الأساسية: أوصل إلى كل عميل الأحداث التي تهمه فقط باستخدام التصفية من جانب الخادم، ووسّع نطاق الاشتراكات عبر عدة مثيلات من Spring Boot. تتمرن على GraphQL APIs with Spring Boot مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ GraphQL APIs with Spring Boot؟

لا تُشترط خبرة سابقة. GraphQL APIs with Spring Boot على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تصفية الاشتراكات وتوسيع نطاقها»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس GraphQL APIs with Spring Boot هذا؟

نعم. كل درس في GraphQL APIs with Spring Boot يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. فهم GraphQL Subscriptions
  2. تطبيق التحديثات في الوقت الفعلي
  3. دمج WebSockets مع Spring
  4. تصفية الاشتراكات وتوسيع نطاقها
← العودة إلى GraphQL APIs with Spring Boot