title: Producing Server Sent Events in Spring Boot Webflux
body: <p>I have the following code in a SpringBoot application:</p>
  <pre><code>class MySse {
      public Mono&lt;ServerResponse&gt; emitEvents(ServerRequest request){
          return ServerResponse.ok()
                  .contentType(MediaType.TEXT_EVENT_STREAM)
                  .body(Mono.from(Flux.interval(Duration.ofSeconds(1))
                  .map(sequence  -&gt; ServerSentEvent.builder()
                          .id(String.valueOf(sequence))
                          .event(&quot;periodic-event&quot;)
                          .data(&quot;SSE - &quot; + LocalTime.now().toString())
                          .build())), ServerSentEvent.class);
      }
  }
  
  @Configuration
  public class MyRoutesConfiguration {
  
      @Autowired
      @Bean
      RouterFunction&lt;ServerResponse&gt; sseRoute(MySse mySse) {
          return route(path(&quot;/sse&quot;).and(method(HttpMethod.GET)), MySse ::emitEvents)
                  ;
      }
  
      @Bean
      public MySse mySse() {
          return new MySse();
      }
  }
  </code></pre>
  <p>If I navigate to http://localhost (route not shown above but it works)
  from there I open DevTools in Chrome and type in the following JavaScript code:</p>
  <pre><code>const evtSource = new EventSource(&quot;sse/&quot;);
  evtSource.onmessage = function(event) {
    console.log(event.data);
  }
  </code></pre>
  <p>But nothing is printed out...</p>
  <p>Breakpoints in the <code>map(...)</code> lambda in <code>MySse::emitEvent</code> are hit every second</p>
  <p>But nothing is printed out in the browswer's JS console.</p>
  <p>if I visit http://localhost/sse I get the following response:</p>
  <pre><code>id:0
  event:periodic-event
  data:SSE - 20:17:12.972706400
  </code></pre>