Subscription

Definition

A subscription is a concept used in various programming paradigms to manage the flow of data between a producer and a consumer. In the context of Reactive Streams, a subscription represents the relationship between a subscriber and a producer. The subscriber has control over when elements are requested and when more elements are no longer required. This is managed through the request() method, which is used to request data, and the cancel() method, which is used to cancel subscriptions.

In GraphQL, a Subscription is a type that allows clients to subscribe to real-time updates from the server. It is used to push data to clients when certain events occur on the server.

Reactive Streams API

The Reactive Streams API is a standard for asynchronous stream processing with non-blocking backpressure. It defines four main interfaces: Publisher, Subscriber, Subscription, and Processor. These interfaces facilitate the communication and data flow between different components in a reactive system.

[Listing 8.1 Reactive Streams API](https://livebook.manning.com/book/spring-boot-in-practice/chapter-8/44)

public interface Publisher<T> {
   public void subscribe(Subscriber<? super T> s);
}
  
public interface Subscriber<T> {
   public void onSubscribe(Subscription s);
   public void onNext(T t);
   public void onError(Throwable t);
   public void onComplete();
}
  
public interface Subscription {
   public void request(long n);
   public void cancel();
}
 
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {
}

[Figure 8.7 Communication in Reactive Streams API](https://livebook.manning.com/book/spring-boot-in-practice/chapter-8/51)

Figure 8.7 Communication between publisher, subscriber, and subscription interfaces in Reactive Streams API Figure 8.7 Communication between publisher, subscriber, and subscription interfaces in Reactive Streams API

This figure illustrates the communication flow between the Publisher, Subscriber, and Subscription interfaces in the Reactive Streams API.

GraphQL Subscriptions

GraphQL subscriptions enable real-time data updates from the server to the client. They are particularly useful for applications that require live updates, such as chat applications or live sports scores.

[Listing 10.38 The Subscription Type Definition](https://livebook.manning.com/book/spring-boot-in-practice/chapter-10/174)

type Subscription {
    reviewEvents (courseId: Int) : Review
}

In this listing, a GraphQL subscription type called reviewEvents is defined. It accepts an argument courseId of type Int and returns a Review.

[Listing 10.39 The ReviewEvents Subscription Mapping](https://livebook.manning.com/book/spring-boot-in-practice/chapter-10/174)

@SubscriptionMapping
Flux<Review> reviewEvents(@Argument Integer courseId) {
    return this.courseRepository.findById(courseId)
            .flatMapMany(review ->
➥ this.reviewRepository.findByCourseId(review.getId()))
            .delayElements(Duration.ofSeconds(1))
            .take(5);
}

This listing shows the implementation of the reviewEvents subscription mapping in a Spring controller class. The method accepts a courseId argument, retrieves the corresponding course, and sends back available reviews with a delay of one second between each, taking a maximum of five reviews.

[Figure 10.18 GraphQL Subscription Mapping Output](https://livebook.manning.com/book/spring-boot-in-practice/chapter-10/282)

Figure 10.18 Browser console output of the GraphQL subscription mapping. The reviews are printed in one second. Figure 10.18 Browser console output of the GraphQL subscription mapping. The reviews are printed in one second.

This figure shows the output of the GraphQL subscription mapping in the browser console, where reviews are printed every second.