Class EventSource

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class EventSource
    extends java.lang.Object
    implements java.io.Closeable
    A client for the Server-Sent Events (SSE) protocol.

    Instances are always configured and constructed with EventSource.Builder. The client is created in an inactive state and will not connect until you call start().

    Note that although EventSource is named after the JavaScript API that is described in the SSE specification, its behavior is not identical to standard web browser implementations of EventSource, specifically in terms of failure/reconnection behavior: it will automatically retry (with a backoff delay) for some error conditions where a browser will not retry. It also supports request configuration options (such as request headers and method) that the browser EventSource does not support. However, its interpretation of the stream data is fully conformant with the SSE specification, unless you use the opt-in mode EventSource.Builder.streamEventData(boolean) which allows for greater efficiency in some use cases but has some behavioral constraints.

    • Method Detail

      • start

        public void start()
        Attempts to connect to the remote event source if not already connected. This method returns immediately; the connection happens on a worker thread.
      • restart

        public void restart()
        Drops the current stream connection (if any) and attempts to reconnect.

        This method returns immediately after dropping the current connection; the reconnection happens on a worker thread.

        If a connection attempt is already in progress but has not yet connected, or if close() has previously been called, this method has no effect. If start() has never been called, it is the same as calling start().

      • getState

        public ReadyState getState()
        Returns an enum indicating the current status of the connection.
        Returns:
        a ReadyState value
      • close

        public void close()
        Drops the current stream connection (if any) and permanently shuts down the EventSource.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • awaitClosed

        public boolean awaitClosed​(long timeout,
                                   java.util.concurrent.TimeUnit timeUnit)
                            throws java.lang.InterruptedException
        Block until all underlying threads have terminated and resources have been released.
        Parameters:
        timeout - maximum time to wait for everything to shut down, in whatever time unit is specified by timeUnit
        timeUnit - the time unit, or TimeUnit.MILLISECONDS if null
        Returns:
        true if all thread pools terminated within the specified timeout, false otherwise.
        Throws:
        java.lang.InterruptedException - if this thread is interrupted while blocking
      • getLastEventId

        public java.lang.String getLastEventId()
        Returns the ID value, if any, of the last known event.

        This can be set initially with EventSource.Builder.lastEventId(String), and is updated whenever an event is received that has an ID. Whether event IDs are supported depends on the server; it may ignore this value.

        Returns:
        the last known event ID, or null
        Since:
        2.0.0
        See Also:
        EventSource.Builder.lastEventId(String)
      • getHttpUrl

        public okhttp3.HttpUrl getHttpUrl()
        Returns the current stream endpoint as an OkHttp HttpUrl.
        Returns:
        the endpoint URL
        Since:
        1.9.0
        See Also:
        getUri()
      • getUri

        public java.net.URI getUri()
        Returns the current stream endpoint as a java.net.URI.
        Returns:
        the endpoint URI
        See Also:
        getHttpUrl()