Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 27, 2025. It is now read-only.

streamdataio/eventsource-android

Repository files navigation

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.

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);
 /* eventSource = new EventSource(UriProxy, UriApi, new SSEHandler(), extraHeaderParameters)*/
 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 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.

About

a simple eventsource implementation for android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages

  • Java 100.0%

AltStyle によって変換されたページ (->オリジナル) /