STOMP protocol via WebSocket for Android

Posted By : Prince Bhardwaj | 29-Nov-2017

Stomp protocol android library provide the support for STOMP protocol.This library works works as a client for backend with the support of STOMP, just like NodeJS(stomjs or other) or Spring Boot(SockJS).

Adding library as a gradle dependency

 repositories { 
    jcenter()
    maven { url "https://jitpack.io" }
}
dependencies {
    compile 'com.github.NaikSoftware:StompProtocolAndroid:{latest version}'
}


 


Following is how to use the library

   import org.java_websocket.WebSocket;

 private StompClient mStompClient;
 
 // ...
 
 mStompClient = Stomp.over(WebSocket.class, "ws://10.0.2.2:8080/example-endpoint/websocket");
 mStompClient.connect();
  
 mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
     Log.d(TAG, topicMessage.getPayload());
 });
  
 mStompClient.send("/topic/hello-msg-mapping", "My first STOMP message!").subscribe();
  
 // ...
 
 mStompClient.disconnect();



The method Stomp.over consumes class for creating connection as first parameter.You should provide dependencies for lib and pass class.Now supported connection providers 
    
    org.java_websocket.WebSocket.class ('org.java-websocket:Java-WebSocket:1.3.0')
okhttp3.WebSocket.class ('com.squareup.okhttp3:okhttp:3.8.0')

we can add our own connection provider simply implement the ConnectionProvider interface.If you have provided new provider create pull

Subscribe lifecycle connection

mStompClient.lifecycle().subscribe(lifecycleEvent -> {
    switch (lifecycleEvent.getType()) {
    
        case OPENED:
            Log.d(TAG, "Stomp connection opened");
            break;
            
        case ERROR:
            Log.e(TAG, "Error", lifecycleEvent.getException());
            break;
            
        case CLOSED:
             Log.d(TAG, "Stomp connection closed");
             break;
    }
});
 

About Author

Author Image
Prince Bhardwaj

Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.

Request for Proposal

Name is required

Comment is required

Sending message..