Event sent by server does not work with SSE tweet

I am trying to use JavaScript SSE from Jersey. I have the following code in my resource. I host on Java7 and Tomcat 7. I am not getting any errors. But I do not see the data on the page.

I call /broadcast to send data. It shows a message. But nothing comes to the client. In Firefox, I see the /broadcast event several times.

This is the link I used. https://jersey.java.net/documentation/latest/sse.html

  package net.jigarshah.dse.tracker; import javax.inject.Singleton; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.glassfish.jersey.media.sse.EventOutput; import org.glassfish.jersey.media.sse.OutboundEvent; import org.glassfish.jersey.media.sse.SseBroadcaster; import org.glassfish.jersey.media.sse.SseFeature; @Singleton @Path("broadcast") public class SSEResource { private SseBroadcaster broadcaster = new SseBroadcaster(); @POST @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN) public String broadcastMessage(String message) { OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder(); message = message + "\n\n"; OutboundEvent event = eventBuilder.name("message") .mediaType(MediaType.TEXT_PLAIN_TYPE) .data(String.class, message) .build(); broadcaster.broadcast(event); System.out.println("broadcasting listen [" +message+ "]"); return "Message was '" + message + "' broadcast."; } @GET @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput listenToBroadcast() { System.out.println("will listen"); final EventOutput eventOutput = new EventOutput(); this.broadcaster.add(eventOutput); return eventOutput; } } 

My Index.html code is as follows.

 <script type="text/javascript"> var url = "webapi/broadcast"; //var url="http://localhost:8080/trackapp/webapi/broadcast/listen"; var source=new EventSource(url); source.onerror=function(event) { console.log("error [" + source.readyState + "]"); }; source.onopen = function(event){ console.log("eventsource opened!"); }; source.onmessage=function(event) { console.log(event.data); document.getElementById("result").innerHTML+=event.data + "<br>"; }; </script> 
+4
source share
4 answers

I had the same problem and I decided not to specify the event name (I don’t know why, but this seems to be the solution) ... here is the code

 OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder(); //WARNING: IF I SET THE NAME OF THE EVENT IT DOES NOT WORK //eventBuilder.name("message"); eventBuilder.mediaType(MediaType.APPLICATION_JSON_TYPE); eventBuilder.data(EventData.class, data); OutboundEvent event = eventBuilder.build(); 
+9
source

The OutboundEvent.Builder.name method sets the value of the SSE event field. Therefore, it is not considered as a message and cannot be listened with the .onmessage handler. The solution simply uses the addEventListener method to register the listener method.

This code should work with the jersey sample above.

 <script type="text/javascript"> var url = "webapi/broadcast"; var source=new EventSource(url); source.addEventListener( "message", function(event){ console.log(event.data); document.getElementById("result").innerHTML+=event.data + "<br>"; }, false); </script> 

I found this article helpful in understanding how SSE works in a browser: http://www.sitepoint.com/server-sent-events/ .

+5
source

Here is an example that may come in handy: http://en.kodcu.com/2013/11/jaxrs-2-html-5-server-sent-events-on-glassfish-4/

And you can also refer to this page to find out if your browser supports the EventSource API http://www.eventsourcehq.com/browser-support

+2
source

I have to do the following so that it works with tomcat 7.0.69 and java 1.8, and jersey 2.22.2 Just share it if it comes to any use of any body.

  • Add the following dependency to the Pom file:

      <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.22.2</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.22.2</version> </dependency> <!-- If you use json --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-processing</artifactId> <version>2.22.2</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" --> <artifactId>jersey-container-servlet</artifactId> <version>2.22.2</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-sse</artifactId> <version>2.22.2</version> </dependency> 
  • You need to be servlet 3, so the web.xml header should look like this: < ?xml version="1.0" encoding="UTF-8"? > < web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version="3.0" > < ?xml version="1.0" encoding="UTF-8"? > < web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version="3.0" >

  • The container for Jersey servlets must be supported asynchronously, so add it to the Jersey sergeant in web.xml:

<asynchronous supports> true </ async-support>

  1. If you use any filter, make sure it is also supported by async:

    <asynchronous supports> true </ asynchronous with support>

0
source

All Articles