An extension library for OkHttp to create a Server-Sent Event (SSE) client.
Switch branches/tags
Nothing to show
Clone or download
skimarxall Merge pull request #25 from T-Spoon/master
Allow HTTP/2 connections
Latest commit 2d72172 Oct 9, 2018
Failed to load latest commit information.
src Merge pull request #25 from T-Spoon/master Oct 9, 2018
.gitignore Initial commit Aug 1, 2017
.travis.yml Add a simple Travis CI file Aug 1, 2017
LICENSE Initial commit Aug 1, 2017 Update Jun 19, 2018
pom.xml Bump OkHttp to 3.9.1 Jan 2, 2018

Build Status


OkSse is an extension library for OkHttp to create a Server-Sent Event (SSE) client

Server-sent events is a standard describing how servers can initiate data transmission towards clients once an initial client connection has been established. They are commonly used to send message updates or continuous data streams to a client.


Add JitPack repository:

allprojects {
    repositories {
        maven { url '' }

Add OkSse dependency:

dependencies {
    implementation 'com.github.heremaps:oksse:0.9.0'


You can create an OkSse instance either using an already existing OkHttp client instance, or let OkSse create a default one instead.

The following code creats a request that points to our SSE server and uses the OkSse instance to create a new ServerSentEvent connection:

Request request = new Request.Builder().url(path).build();
OkSse okSse = new OkSse();
ServerSentEvent sse = okSse.newServerSentEvent(request, listener);

This implements the ServerSentEvent listener to get notified of the channel status and the received messages or comments:

new ServerSentEvent.Listener() {
    public void onOpen(ServerSentEvent sse, Response response) {
        // When the channel is opened

    public void onMessage(ServerSentEvent sse, String id, String event, String message) {
        // When a message is received

    public void onComment(ServerSentEvent sse, String comment) {
       // When a comment is received

    public boolean onRetryTime(ServerSentEvent sse, long milliseconds) {
        return true; // True to use the new retry time received by SSE

    public boolean onRetryError(ServerSentEvent sse, Throwable throwable, Response response) {
        return true; // True to retry, false otherwise

    public void onClosed(ServerSentEvent sse) {
        // Channel closed

When done listing from SSE, remember to close the channel and clear resources:


Once closed, you will need to create a new instance.

Alternative constructor

You can provide your own OkHttp client when creating a new OkSse instance.

OkHttpClient client = new OkHttpClient.Builder().readTimeout(0, TimeUnit.SECONDS).build();
OkSse oksse = new OkSse(client)

Note the read timeout set to 0, this is required in order to keep the connection alive, if not OkHttp will close the channel after timeout is reached.


Copyright (c) 2017 HERE Europe B.V.

Please see the LICENSE file for details.