Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Socket.IO best practices for keeping connection alive

My Android app is using the AndroidAsync library to connect to a Node.js server with the Socket.IO client. The socket client is established through a service.

  • Socket needs to stay open/connected when app is open
  • Socket can close when app is not open
  • There is one exception where socket needs to stay open while app is not open

I am currently starting the Socket.IO service in the onResume and stopping in onPause of each Activity in the app.

This seems really inefficient because I basically stopping the socket and recreating a new one every time I press the home button, or switch to another activity in the app.

What would be the best way to handle the above requirements about keeping the socket open?

public class SocketIOService extends Service {

    private Preferences prefs;
    private SocketIOClient socket;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            ConnectCallback callback = new ConnectCallback() {
                @Override
                public void onConnectCompleted(Exception ex, SocketIOClient client) {
                    if (ex != null) {
                        ex.printStackTrace();
                        return;
                    }

                    Log.v("SOCKET IO CONNECTION", "ESTABLISHED");

                    client.setDisconnectCallback(new DisconnectCallback() {
                        @Override
                        public void onDisconnect(Exception e) {
                            Log.v("SOCKET IO CONNECTION", "TERMINATED");
                        }
                    });

                    client.setErrorCallback(new ErrorCallback() {
                        @Override
                        public void onError(String error) {
                            Log.e("SOCKET IO ERROR", error);
                        }
                    });

                    client.setExceptionCallback(new ExceptionCallback() {
                        @Override
                        public void onException(Exception e) {
                            e.printStackTrace();
                        }
                    });

                    client.setReconnectCallback(new ReconnectCallback() {
                        @Override
                        public void onReconnect() {
                            Log.v("SOCKET IO CONNECTION", "RECONNECTED");
                        }
                    });

                    client.on(EVENT_NEWS, new EventCallback() {
                        @Override
                        public void onEvent(JSONArray argument, Acknowledge acknowledge) {
                            Log.v("EVENT:NEWS", argument.toString());
                        }
                    });

                    client.on(EVENT_MESSAGE_RECEIVE, new EventCallback() {
                        @Override
                        public void onEvent(JSONArray argument, Acknowledge acknowledge) {
                            handleMessageReceive(argument);
                        }
                    });

                }
            };

            socket = SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), URL_SERVER, callback).get();

            JSONArray array = new JSONArray();
            JSONObject obj = new JSONObject();
            prefs = new Preferences(this);
            try {
                obj.put(KEY_USER_ID, prefs.getUserId());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            array.put(obj);
            socket.emit(EVENT_LOG_USER_ID, array);

            Log.v("SOCKET LOG USER ID", array.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        // If we get killed, after returning from here, restart
        return START_REDELIVER_INTENT;
    }

    @Override
    public void onDestroy() {
        Log.v("SOCKET IO SERVICE", "STOPPED");
        if (socket != null) {
            if (socket.isConnected()) {
                socket.disconnect();
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void handleMessageReceive(JSONArray json) {
        ChatMessage message = JReader.createMessage(json);
    }

}
like image 945
The Nomad Avatar asked Apr 22 '14 22:04

The Nomad


1 Answers

Looking into old unanswered questions and found this one. I think the best approach is to use a bound Service that autostarts.

Using the bindService() you can bind your Activities in onResume() or onStart() and let your service close the connection and stop itself when there is no client bound on it.

You can find more information here https://developer.android.com/guide/components/bound-services

like image 156
Klitos G. Avatar answered Sep 20 '22 15:09

Klitos G.