- Home
- Documentation
- Use the Java EventSource API
Note: To use the Gateway, a KAAZING client library, or a KAAZING demo, fork the repository from kaazing.org.
This procedure describes how you can use the EventSource API--provided by the Kaazing Java client library--in Java. This API allows you to take advantage of the server-sent events standard as described in the HTML5 specification. For example, you can create a Java applet or stand-alone Java application that uses the Java HTML5 Communications client library to receive streaming data from a news feed or streaming financial data. The support for server-sent events is provided by the EventSource class and its supporting classes.
The following steps show you how to use the EventSource API in a Java applet or stand-alone Java application. This example highlights some of the most commonly used EventSource methods and is not meant to be an end-to-end tutorial. Refer to the EventSource API documentation for a complete description of all the available methods. View the out of the box Server Sent Events demo code in GATEWAY_HOME/demo/java/src/core/com/kaazing/net/sse/demo/ServerSentEventsApplet.java. The example code below is taken from this demo.
This procedure is part of Build Java WebSocket Clients:
- Set Up Your Development Environment
- Use the Java WebSocket API
- Use the Java EventSource API
- Migrate WebSocket and ByteSocket Applications to KAAZING Gateway 5.0
- Secure Your Java and Android Clients
- Display Logs for the Java Client
- Troubleshoot Your Java Client
-
Add the necessary import statements:
// Import java.net classes import java.net.URI; import java.net.URL; // Import EventSource API classes import org.kaazing.net.sse.SseEventReader; import org.kaazing.net.sse.SseEventSource; import org.kaazing.net.sse.SseEventSourceFactory; import org.kaazing.net.sse.SseEventType;
-
Create a new SseEventSource object and connection using the Server Sent Events factory class:
SseEventSource eventSource = null; // Create a variable for the Event Source // Create Event Source factory SseEventSourceFactory factory = SseEventSourceFactory.createEventSourceFactory(); // Create a target location using the java.net.URI create() method eventSource = factory.createEventSource(URI.create(url.getText())); // Connect to the event source. eventSource.connect();
-
Use the SseEventReader class to create a new reader object, and then use
getEventReader()method to receive events:Thread sseEventReaderThread = new Thread() { public void run() { try { SseEventReader reader = eventSource.getEventReader(); // Receive event stream SseEventType type = null; while ((type = reader.next()) != SseEventType.EOS) { // Wait until type is DATA switch (type) { case DATA: // Return the payload of the last received event logArea.setText("<html>" + reader.getData() + "</html>"); break; case EMPTY: logArea.setText("<html>" + "</html>"); break; } } } catch (Exception ex) { logArea.setText("Exception: " + ex.getMessage()); } } };
Notes:
- The SseEventReader class has a
next()method that causes the thread to block until an event is received. - The
getData()method returns the payload of the last received event. This is not a blocking call. This method should be invoked afternext()only if the returned type isSseEventType.DATA. Otherwise, an IOException is thrown. - Receiving events is performed in a separate thread. In UI-based Java clients, receiving events should be done on a separate thread to avoid blocking the java.awt EventDispatchThread.
- A switch block is used to manage different types of events. The SseEventType class is used to identify the type. It has three values:
EOSfor end of stream,EMPTYfor empty events, andDATAfor events.
- The SseEventReader class has a
-
Later, you can call the
close()method in case you want to stop listening to messages from a particular event source.eventSource.close();
Migrate WebSocket and ByteSocket Applications to KAAZING Gateway 5.0