Search code examples
androidwebsocketstomp

Suggest me to retrieve real time data from websocket server


I am currently working on an GPS based project, I am currently using volley to receive data from web but recently they plan to upgrade the services with real time data. so web developers have programmed the server using websocket, and for browser application they said, they are using stomp.js protocol for retrieving data from server. They requested me to retrieve data from their server (Real Time Data) use any, but I don't know what will be the best suitable method to get real time data from websocket. I found one example from github https://github.com/NaikSoftware/StompProtocolAndroid but it's not enough for me to learn, and documentation for using stomp are not enough on internet. I hope there could be better implementation in android.

Please any help and suggestion would be appreciated. Please don't mention me firebase because I have already mentioned them about firebase and they refused to use it. Please guys help me, suggest me or give me one good example to simply retrieve data from websocket server using volley..


Solution

  • My answer is based on the comments in this question. Don't have enough information on the question so I am writing 2 solution for your question

    Solution 1 - Without using Stomp Implementation- Simple websocket

    This is a simple Websocket implementation. for this you can use koush/AndroidAsync library

    implement this project into your project

        dependencies {
        compile 'com.koushikdutta.async:androidasync:2.+'
    }
    

    then connect to Websocket server

     String url="ws://172.17.1.54:8000/";
        AsyncHttpClient.getDefaultInstance().websocket(url, "my-protocol", new WebSocketConnectCallback() {
            @Override
            public void onCompleted(Exception ex, WebSocket webSocket) {
                if (ex != null) {
                    ex.printStackTrace();
                    return;
                }
    
                webSocket.setStringCallback(new StringCallback() {
                    public void onStringAvailable(String s) {
                        System.out.println("I got a string: " + s);
                    }
                });
                webSocket.setDataCallback(new DataCallback() {
                    public void onDataAvailable(DataEmitter emitter, ByteBufferList byteBufferList) {
                        System.out.println("I got some bytes!");
                        // note that this data has been read
                        byteBufferList.recycle();
                    }
                });
            }
        });
    

    here setStringCallback() & setDataCallback() will receive your real time update.

    You can also use codebutler/android-websockets library for this.

    Solution 2: With use Stomp implementation

    For this you can use Gozirra java library for solving this problem. You can download client library.then put it into your libs folder.

    For connecting to your server

     Client c = new Client("server url", port, "login", "password");
    

    Create a listener for the updates

     Listener listener=new Listener() {
                @Override
                public void message(Map map, String s) {
                    //Do your stuff
                }
            };
    

    Then Subscribe to your topic messages

     c.subscribe("foo-channel", listener);
    

    If you want to unsubscribe you can use below code

      c.unsubscribe("foo-channel", listener);  // Unsubscribe only one listener
    

    or

     c.unsubscribe("foo-channel");   // Unsubscribe all listeners
    

    For disconnecting client

    c.disconnect();
    

    I didn't test this with a real server .But I think this will work.Please let me know if it solves your problem

    Solution 3

    As you mention the library https://github.com/NaikSoftware/StompProtocolAndroid You can use this. simplify version is as follows

    Add maven link into your project level gradle

      repositories {
            maven { url "https://jitpack.io" }
        }
    

    Add dependency in your module level gradle

     implementation 'com.github.NaikSoftware:StompProtocolAndroid:1.1.5'
     implementation 'org.java-websocket:Java-WebSocket:1.3.0'
    

    Then use following code for getting message

     StompClient mStompClient = Stomp.over(WebSocket.class, "ws://10.0.2.2:5000/");
    
    
        mStompClient.topic("/topic/general").subscribe(new Action1<StompMessage>() {
            @Override
            public void call(StompMessage stompMessage) {
                Log.e(TAG, stompMessage.getPayload());
            }
        });
        mStompClient.lifecycle().subscribe(new Action1<LifecycleEvent>() {
            @Override
            public void call(LifecycleEvent lifecycleEvent) {
                switch (lifecycleEvent.getType()) {
    
                    case OPENED:
                        Log.e(TAG, "Stomp connection opened");
                        break;
    
                    case ERROR:
                        Log.e(TAG, "Error", lifecycleEvent.getException());
                        break;
    
                    case CLOSED:
                        Log.e(TAG, "Stomp connection closed");
                        break;
                }
            }
        });
        mStompClient.connect();