Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a Java implementation of the client side of the Server Sent Events spec. [closed]

I have a desktop client and a server sides both implemented in Java. The server side sits on Tomcat.

Now I wish to check using Server Sent Events and while Tomcat makes it possible to use it on the server, I do not see how can I do it from the client side of it. All the implementations utilize Javascript, which is fine if your client is a Javascript client. Mine is not.

So, my question is this - are there any Java implementations of the client side of the SSE spec?

like image 741
mark Avatar asked Mar 19 '13 09:03

mark


People also ask

How are Server-Sent Events implemented?

The server-side event stream syntax is simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending event streams. header('Content-Type: text/event-stream');

How do SSE events work?

SSE is designed to use the JavaScript EventSource API in order to subscribe to a stream of data in any popular browser. Through this interface a client requests a particular URL in order to receive an event stream. SSE is commonly used to send message updates or continuous data streams to a browser client.

Which element is required in a web application to operate the Server-Sent Events?

To use Server-Sent Events in a web application, you would need to add an <eventsource> element to the document. The src attribute of <eventsource> element should point to an URL which should provide a persistent HTTP connection that sends a data stream containing the events.


1 Answers

I had the same problem and found a solution. Jersey has a SSE client (its not part of the standard distribution, you also need the jersey-media-sse jar which you can find in the Maven repository.

 public static void consumeEventStream(String url, Consumer consumer) throws Exception {
    Client client = ClientBuilder.newBuilder().register(new SseFeature()).build();
    WebTarget target = client.target(url);
    EventInput e = null;
    while (true) {
        Thread.sleep(1000);
        if (e==null || e.isClosed()) {
            // (re)connect
            e = target.request().get(EventInput.class);

            e.setChunkType("text/event-stream");
        }

        final InboundEvent inboundEvent = e.read();
        if (inboundEvent == null) {  
            break;
        }
        else {
           String data = inboundEvent.getData();
           // do something here - notify observers, parse json etc
        }

    }
    System.out.println("connection closed");
}

The invocation of setChunkType is to deal with a bug in Jersey - if the char encoding is part of the content type, jersey will not recognise the event type. On the other hand, the spec and browsers expect the content type to be set. Acc. to jira this has been resolved but is defintely not yet working in the latest version jersey-media-sse-2.2.jar, see also https://java.net/jira/browse/JERSEY-2062.

Hope this helps, Jens

like image 88
jens Avatar answered Oct 23 '22 09:10

jens