GithubHelp home page GithubHelp logo

leeforrest / oksse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from heremaps/oksse

0.0 1.0 0.0 32 KB

An extension library for OkHttp to create a Server-Sent Event (SSE) client.

License: Apache License 2.0

Java 100.00%

oksse's Introduction

Build Status

Introduction

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.

Integration

Add JitPack repository:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add OkSse dependency:

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

Usage

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() {
    @Override
    public void onOpen(ServerSentEvent sse, Response response) {
        // When the channel is opened
    }

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

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

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

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

    @WorkerThread
    @Override
    public void onClosed(ServerSentEvent sse) {
        // Channel closed
    }

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

sse.close();

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.

License

Copyright (c) 2017 HERE Europe B.V.

Please see the LICENSE file for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.