I want to be able to use Server-Side-Events (SSE) in a web service to send a
stream of messages to the browser and I have a Camel application acting as a
reverse-proxy between them. However the Camel route is buffering the messages
sent using SSE and messages are not delivered to the browser until the request
is completed. My question is: which Camel components support SSE and what
configuration is required to achieve streaming of messages using SSE.
Here are the details of what I did. I have configured Camel as a reverse-proxy
between a browser and a web service, using the bridgeEndpoint setting in the
http component. The route works as expected and HTTP requests are served back
to the browser via Camel. Here is my route:
from("servlet:app//"
+ "?matchOnUriPrefix=true")
.to("http://{{ctg.edm.base-url}}"
+ "?bridgeEndpoint=true"
+ "&preserveHostHeader=true"
+ "&throwExceptionOnFailure=false");
As a test, I have implemented SSE in the web service using Spring's SseEmitter
class. When a request is made to a /notifications/subscribe end-point, the
service will send 4 messages at 5 second intervals using the SseEmitter send()
method and then we explicitly close the connection using the SseEmitter
complete() method.
If I configure the browser to make the request directly to the web service, it
works as expected and the browser receives messages at 5 second intervals. If I
direct the browser request via the Camel route I have configured to be the
reverse-proxy, I receive all the messages at the same time and only when the
request has been completed by the server. How do I configure the Camel route to
stream the messages to the browser instead of buffering them?
I'm using Camel 3.6.0 in a Spring Boot application to implement the
reverse-proxy.
Here are the response headers for one of the messages, as logged by my Camel
application. You can see that the headers are set correctly for SSE, i.e.
Content-Type: text/event-stream, Transfer-Encoding: chunked, Cache-Control:
no-cache, Connection: keep-alive. The Camel logs are showing the messages being
received by the route at 5 second intervals but they are being buffered by
Camel until after the response has been closed.
2020-10-22 11:20:32.825 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "HTTP/1.1 200 [\r][\n]"
2020-10-22 11:20:32.825 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Pragma: no-cache[\r][\n]"
2020-10-22 11:20:32.825 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Expires: Thu, 01 Jan 1970 00:00:00
GMT[\r][\n]"
2020-10-22 11:20:32.825 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Cache-Control: no-cache[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Cache-Control: no-store[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "X-XSS-Protection: 1;
mode=block[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "X-Frame-Options: SAMEORIGIN[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "X-Content-Type-Options:
nosniff[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Content-Type:
text/event-stream;charset=UTF-8[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Transfer-Encoding: chunked[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Date: Thu, 22 Oct 2020 11:20:32
GMT[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Keep-Alive: timeout=20[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "Connection: keep-alive[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "5[\r][\n]"
2020-10-22 11:20:32.826 DEBUG 21995 --- [io-8082-exec-35] org.apache.http.wire
: http-outgoing-33 << "data:[\r][\n]"
Many thanks for your help,
David
Enterprise Architecture Solutions Ltd
Telephone: +44 (0) 20 7788 7708
Web: www.enterprise-architecture.com<http://www.enterprise-architecture.com>
Proud sponsors of The Essential Project
The free open-source Enterprise Architecture Management Platform
www.enterprise-architecture.org
[Essential Project Logo]
________________________________
This message is confidential and may be privileged. It is for the exclusive use
of the intended recipient(s). If you are not the intended recipient(s) please
notify the sender and delete the message immediately. Unauthorised disclosure,
distribution and copying of this email is strictly prohibited. The opinions
expressed within this message are those of the individual author. Whilst
Enterprise Architecture Solutions Ltd take reasonable steps to scan this email
it does not accept liability for any virus that may be contained in it.
Enterprise Architecture Solutions Ltd is registered in England and Wales:
04097721. Registered Office: Suite 428, 39A Barton Road, Water Eaton,
Bletchley, Bucks MK2 2HW.
Enterprise Architecture Solutions Ltd, Tower Bridge Business Centre, 46-48 East
Smithfield, London E1W 1AW, United Kingdom Telephone: +44 (0) 20 7788 7708