title: Spring sseEmitter, events aren't sent right after method send was called
body: <p>I am trying to make server-sent events with Spring 4 (tomcat 7, servlet-api 3.0.1). </p>
  
  <p>The problem is my <code>Events</code> aren't sent right after method send was called. They all come simultaneously (with same timestamp) to client only after timeout of <code>SseEmitter</code>, with <code>EventSource</code>'s error event. And then client is trying to reconnect. Any idea what's happening?</p>
  
  <p>I have created a simple service:</p>
  
  <pre><code>@RequestMapping(value = "subscribe", method = RequestMethod.GET)
  public SseEmitter subscribe () throws IOException {
      final SseEmitter emitter = new SseEmitter();
      Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              try {
                  emitter.send(SseEmitter.event().data("Thread writing: " + Thread.currentThread()).name("ping"));
              } catch (Exception e) {
              }
          }
      } , 1000, 1000, TimeUnit.MILLISECONDS);
      return emitter;
  }
  </code></pre>
  
  <p>with client code:</p>
  
  <pre><code>sse = new EventSource(urlBuilder(base, url));
  sse.addEventListener('ping', function (event) {
      dfd.notify(event);
  });
  
  sse.addEventListener('message', function(event){
      dfd.notify(event);
  });
  
  sse.addEventListener('close', function(event){
      dfd.notify(event);
  });
  
  sse.onerror = function (error) {
      console.log(error);
  };
  
  sse.onmessage = function (event){
      dfd.notify(event);
  };
  </code></pre>
  
  <p>App initalizer code</p>
  
  <pre><code>public class WebAppInitializer implements WebApplicationInitializer {
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
          ctx.register(AppConfig.class);
          ctx.setServletContext(servletContext);
          ctx.refresh();
  
          ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
          dynamic.setAsyncSupported(true);
          dynamic.addMapping("/api/*");
          dynamic.setLoadOnStartup(1);
          dynamic.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
  
          javax.servlet.FilterRegistration.Dynamic filter = servletContext
                  .addFilter("StatelessAuthenticationFilter",
                          ctx.getBean("statelessAuthenticationFilter", StatelessAuthenticationFilter.class));
          filter.setAsyncSupported(true);
          filter.addMappingForUrlPatterns(null, false, "/api/*");
  
          filter = servletContext.addFilter("HibernateSessionRequestFilter",
                  ctx.getBean("hibernateSessionRequestFilter", HibernateSessionRequestFilter.class));
          filter.setAsyncSupported(true);
          filter.addMappingForUrlPatterns(null, false, "/api/user/*");
      }
  }
  </code></pre>
  
  <p>AppConfig.java</p>
  
  <pre><code>@Configuration
  @ComponentScan("ru.esoft.workflow")
  @EnableWebMvc
  @PropertySource({"classpath:mail.properties", "classpath:fatclient.properties"})
  @EnableAsync
  @EnableScheduling
  public class AppConfig extends WebMvcConfigurerAdapter {
  ...
  }
  </code></pre>
  
  <p>Image of my client log:
  <a href="https://i.stack.imgur.com/QY7ha.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QY7ha.png" alt="enter image description here"></a></p>