Vikhyath Maiya
Vikhyath Maiya

Reputation: 3192

Server Sent Events not working in Chrome

i am developing a web application with angular 4 and java. I am using server sent events to send some text data to the frontend. In the backend i am using jersey and this is my backend code

eventOutput.write(new OutboundEvent.Builder()
       .id(decodedToken)
       .name("responseBody")
       .data(String.class, respBody.toString()).build());

Frontend i am using angular 4 and the code looks like

var evtSource = new EventSource("url");
 source.addEventListener('eventName', (event) => {
    console.log(event);
});

Everything works fine in Mozilla firefox. When i use chrome with tomcat and windows everything works fine. But with tomcat+linux and chrome, i see the error in the console.as

EventSource's response has a charset ("iso-8859-1") that is not UTF-8. Aborting the connection.

What could be the issue. What is the fix? Please help. Any help would be appreciated. Thanks.

Upvotes: 9

Views: 2332

Answers (2)

Ori Marko
Ori Marko

Reputation: 58774

If the issue is with tomcat in linux, you can update bin\startup.sh the following line

export JAVA_OPTS="-Dfile.encoding=utf-8"

See Force tomcat to use UTF-8 in linux tutorial

Upvotes: 4

Arielle Nguyen
Arielle Nguyen

Reputation: 3162

Tomcat supports charset : iso-8859-1 by default. Please try

@Produces("application/json;charset=UTF-8")
eventOutput.write(new OutboundEvent.Builder()
       .id(decodedToken)
       .name("responseBody")
       .data(String.class, respBody.toString()).build());

Upvotes: 4

Related Questions