Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server sent events in Android

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.

like image 268
Henrique Avatar asked Sep 19 '13 19:09

Henrique


People also ask

How do Server-Sent Events work?

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.

What is Server-Sent Events in spring boot?

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.

What are server side events?

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.


1 Answers

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();
}
like image 67
Jp_ Avatar answered Oct 10 '22 16:10

Jp_