danielmalmq/eventsource-android


Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.danielmalmq:eventsource-android:1.0.0'
	}
	dependencies {
		implementation("com.github.danielmalmq:eventsource-android:1.0.0")
	}
	<dependency>
	    <groupId>com.github.danielmalmq</groupId>
	    <artifactId>eventsource-android</artifactId>
	    <version>1.0.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.danielmalmq" % "eventsource-android" % "1.0.0"
        
        

                            
    :dependencies [[com.github.danielmalmq/eventsource-android "1.0.0"]]
        
        

Readme


eventsource-android

An Android EventSource (SSE) Library This is a Java implementation of the EventSource - a client for Server-Sent Events. The implementation is based on Netty

This project is based of off EventSource-Java: https://github.com/aslakhellesoy/eventsource-java https://github.com/TomMettam/eventsource-java

One addition made to the original source is that headers can now be passed in the method to include authorization tokens, etc in the request.

Note: In order to use eventsource, you must create and connect the event source from a separate thread. If you are planning to update a view's ui from the handler, you will need to use runOnUI or create a handler tied to the main thread.

I've packaged the code into an android library. Just assembleRelease in gradle to create the neccessary AAR file. In the near future, I will upload the library to jcenter.

Example implementation:

Thread eventThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                eventSource = new EventSource(Uri, new SSEHandler(), extraHeaderParameters, true);
                eventSource.connect();
            } catch(URISyntaxException e) {
                Log.v("Error starting eventsource", "True");
            }
        }
    });
}
eventThread.start();


private class SSEHandler implements EventSourceHandler {

    public SSEHandler() {
    }
    
    @Override
    public void onConnect() {
        Log.v("SSE Connected", "True");
    }

    @Override
    public void onMessage(String event, MessageEvent message) {
        Log.v("SSE Message", event);
        Log.v("SSE Message: ", message.lastEventId);
        Log.v("SSE Message: ", message.data);
    }

    @Override
    public void onComment(String comment) {
       //comments only received if exposeComments turned on
       Log.v("SSE Comment", comment);
    }

    @Override
    public void onError(Throwable t) {
        Log.v("SSE Error", "True");
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        Log.v("SSE Stacktrace", sw.toString());

    }

    @Override
    public void onClosed(boolean willReconnect) {
        Log.v("SSE Closed", "reconnect? " + willReconnect);
    }

To stop event source, make sure to run eventSource.close(), as well as remove the handler and thread instance.