====== Spring HTTP Streaming ====== * [[html:html5:websocket|WebSocket]]과 유사한 효과를 낼 수 있는 방식 * [[springframework:webflux|Spring WebFlux]] 사용시 더 깔끔하다. ===== ServerSentEvent Reverse Proxy(nginx) 설정 ===== * [[:nginx|Ngnix]] 같은 reverse proxy 사용시에 buffering 을 끄는 등의 별도 설정이 필요하다. * [[https://serverfault.com/questions/801628/for-server-sent-events-sse-what-nginx-proxy-configuration-is-appropriate|For Server-Sent Events (SSE) what Nginx proxy configuration is appropriate? - Server Fault]] * [[https://github.com/wandenberg/nginx-push-stream-module|wandenberg/nginx-push-stream-module: A pure stream http push technology for your Nginx setup. Comet made easy and really scalable.]] * 서버측에서 ''X-Accel-Buffering: no'' 응답 헤더를 내려주면 SSE 커넥션만 버퍼링 방지. * [[https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/|nginx x-accel-* 헤더를 통한 응답 제어]] @RequestMapping("/sseExamples") public SseEmitter getRealTimeJudgeResultAction(HttpServletResponse response) throws IOException { // .... response.addHeader("X-Accel-Buffering", "no"); SseEmitter sseEmitter = new SseEmitter(); // .... sseEmitter.send("Established"); return sseEmitter; } # 응답 결과를 보면 X-Accel-Buffering: no 헤더가 출력됨. curl -D - http://.... HTTP/1.1 200 X-Accel-Buffering: no Content-Type: text/event-stream Transfer-Encoding: chunked ... * nginx 의 SSE 관련 url 프록시에 대해서만 ''proxy_buffering off;'' 및 기타 설정. * [[https://stackoverflow.com/questions/13672743/eventsource-server-sent-events-through-nginx|ruby - EventSource / Server-Sent Events through Nginx - Stack Overflow]] - https://stackoverflow.com/a/13673298/1051402 location /path/to/sse { proxy_pass http://upstream; proxy_buffering off; # X-Accel-Buffering: no 와 같은 효과 proxy_cache off; proxy_set_header Host $host; proxy_set_header Connection ''; proxy_http_version 1.1; chunked_transfer_encoding off; } * [[https://medium.com/tokopedia-engineering/server-sent-events-26f75e3a5ed2|Server Sent Events. File Upload Progress API using Server… | by Somesh Singh | Tokopedia Engineering | Medium]] : 좀 더 잘 설명돼 있는 듯. ===== 참고 ===== * [[https://supawer0728.github.io/2018/03/15/spring-http-stereamings/|Spring5에서 HTTP Streaming | 기록은 재산이다]] * [[https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/http_streaming.html|Spring MVC - HTTP Streaming using ResponseBodyEmitter]] * [[https://medium.com/@nithinmallya4/processing-streaming-data-with-spring-webflux-ed0fc68a14de|Processing streaming data with Spring WebFlux – Nithin Mallya – Medium]] * [[https://www.baeldung.com/spring-mvc-sse-streams|Spring MVC Streaming and SSE Request Processing | Baeldung]] * [[https://www.baeldung.com/spring-server-sent-events|Server-Sent Events in Spring | Baeldung]] * [[https://dzone.com/articles/event-streaming-using-spring-webflux|Event Streaming Using Spring WebFlux - DZone Java]]