title: Spring Boot Webflux/Netty - Detect closed connection
body: <p>I've been working with spring-boot <code>2.0.0.RC1</code> using the webflux starter (<code>spring-boot-starter-webflux</code>). I created a simple controller that returns a infinite flux. I would like that the Publisher only does its work if there is a client (Subscriber). Let's say I have a controller like this one:</p>
  
  <pre><code>@RestController
  public class Demo {
  
      @GetMapping(value = "/")
      public Flux&lt;String&gt; getEvents(){
          return Flux.create((FluxSink&lt;String&gt; sink) -&gt; {
  
              while(!sink.isCancelled()){
  
                  // TODO e.g. fetch data from somewhere
  
                  sink.next("DATA");
              }
              sink.complete();
          }).doFinally(signal -&gt; System.out.println("END"));
      }
  
  }
  </code></pre>
  
  <p>Now, when I try to run that code and access the endpoint <a href="http://localhost:8080/" rel="noreferrer">http://localhost:8080/</a> with Chrome, then I can see the data. However, once I close the browser the while-loop continues since no cancel event has been fired. <strong><em>How can I terminate/cancel the streaming as soon as I close the browser?</em></strong> </p>
  
  <p>From this <a href="https://stackoverflow.com/a/48202417/9364317">answer</a> I quote that:</p>
  
  <blockquote>
    <p>Currently with HTTP, the exact backpressure information is not
    transmitted over the network, since the HTTP protocol doesn't support
    this. This can change if we use a different wire protocol.</p>
  </blockquote>
  
  <p>I assume that, since backpressure is not supported by the HTTP protocol, it means that no cancel request will be made either.</p>
  
  <p>Investigating a little bit further, by analyzing the network traffic, showed that the browser sends a TCP <code>FIN</code> as soon as I close the browser. Is there a way to configure Netty (or something else) so that a half-closed connection will trigger a cancel event on the publisher, making the while-loop stop?</p>
  
  <p>Or do I have to write my own adapter similar to <code>org.springframework.http.server.reactive.ServletHttpHandlerAdapter</code> where I implement my own Subscriber?</p>
  
  <p>Thanks for any help.</p>
  
  <p><strong>EDIT:</strong>
  An <code>IOException</code> will be raised on the attempt to write data to the socket if there is no client. As you can see in the <a href="https://i.stack.imgur.com/Qw3UU.jpg" rel="noreferrer">stack trace</a>.</p>
  
  <p>But that's not good enough, since it might take a while before the next chunk of data will be ready to send and therefore it takes the same amount of time to detect the gone client. As pointed out in <a href="https://stackoverflow.com/a/48808548/9364317">Brian Clozel's answer</a> it is a known issue in Reactor Netty. I tried to use Tomcat instead by adding the dependency to the <code>POM.xml</code>. Like this:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Although it replaces Netty and uses Tomcat instead, it does not seem reactive due to the fact that the browser does not show any data. However, there is no warning/info/exception in the console. <strong><em>Is <code>spring-boot-starter-webflux</code> as of this version (<code>2.0.0.RC1</code>) supposed to work together with Tomcat?</em></strong></p>