I have a backend server that sends events to clients as Server Sent Events. I haven't been able to find a good library for handling this technology on Android, so I've been using a fallback method of periodically checking the server (via a GET to an events endpoint) for new events.
This is done by a background service every 10 seconds. Needless to say, it's not the best approach. If there aren't any open source libraries already available for this scenario, what is the best approach in terms of memory usage and battery consumption to periodically check a server backend for new events? Is doing a GET to an API end point better or worse than managing an open socket in Android?
I'm open to any suggestions. Thanks.
Server-Sent Events is designed to use the JavaScript EventSource API in order to subscribe to a stream of data in any popular browser. Through this interface a client requests a particular URL in order to receive an event stream.
Simply put, Server-Sent-Events, or SSE for short, is an HTTP standard that allows a web application to handle a unidirectional event stream and receive updates whenever server emits data. Spring 4.2 version already supported it, but starting with Spring 5, we now have a more idiomatic and convenient way to handle it.
Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via an HTTP connection, and describes how servers can initiate data transmission towards clients once an initial client connection has been established.
You can simply use HttpUrlConnection
to make a persistent connection with the server (Android uses keep-alive
by default) and treat the received messages as streams.
public class HttpRequest extends AsyncTask {
@Override
protected Object doInBackground(Object[] params){
try {
URL url = new URL("http://simpl.info/eventsource/index.php");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Log.d("SSE", "http response: " + urlConnection.getResponseCode());
//Object inputStream = urlConnection.getContent();
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
Log.d("SSE reading stream", readStrem(inputStream)+"");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
Log.e("SSE activity", "Error on url openConnection: "+e.getMessage());
e.printStackTrace();
}
return null;
}
}
private String readStrem(InputStream inputStream) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try{
reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while((line = reader.readLine()) != null){
Log.d("ServerSentEvents", "SSE event: "+line);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
return response.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With