0

guys,

I am building a website with JAVA Spring on AWS Elastic beanstalk with Load Balancer. In short, I have a page which receives Server-side Event (SSE) from server using eventsource on client-side and SseEmitter on my Java Spring back-end.

I want my website to work with HTTPS so I followed official suggestion to set the NGINX configuration: https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/?nc1=f_ls

It works well...when I connect to my website with HTTP, it automatically redirect to HTTPS.

However, my server-side event stops working.

I tried solutions from another post: EventSource / Server-Sent Events through Nginx But it didn't help.

On my server side, I also add httpHeaders following other suggestions.

Server-side:

public static final class CustomSseEmitter extends SseEmitter {
    static final MediaType UTF8_TEXT_EVENTSTREAM = new MediaType("text", "event-stream", Charset.forName("UTF-8"));

    @Override
    protected void extendResponse(ServerHttpResponse outputMessage) {
        HttpHeaders headers = outputMessage.getHeaders();
        headers.set("X-Accel-Buffering", "no");
        headers.set("Cache-Control", "no-cache");
        headers.set("Connection", "keep-alive");
        headers.set("Content-Type", "text/event-stream");
        if (headers.getContentType() == null) {
            headers.setContentType(UTF8_TEXT_EVENTSTREAM);
        }
    }
}

Client-side:

var registerSSE = function (companyId, retryCount) {
    source = new EventSource("/middle/registerSSE/" + companyId);

    source.onopen = function (e) {
        console.log("Build SSE successful");
    };

    source.onmessage = function (e) {
        console.log("receive SSE");
    };
    source.onerror = function (e) {
        console.error("SSE broken [" + e.data + "], retry " + (
        ++retryCount) + " times!");
    }
}

Sorry I am kinda a newbie in this context, how can I combine both required features?

Any suggestions would be appreciated. Thanks.

0 Answers0