erobic/StompProtocolAndroid


STOMP protocol via WebSocket for Android

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.erobic:StompProtocolAndroid:1.1.3.3'
	}
	dependencies {
		implementation("com.github.erobic:StompProtocolAndroid:1.1.3.3")
	}
	<dependency>
	    <groupId>com.github.erobic</groupId>
	    <artifactId>StompProtocolAndroid</artifactId>
	    <version>1.1.3.3</version>
	</dependency>

                            
    libraryDependencies += "com.github.erobic" % "StompProtocolAndroid" % "1.1.3.3"
        
        

                            
    :dependencies [[com.github.erobic/StompProtocolAndroid "1.1.3.3"]]
        
        

Readme


STOMP protocol via WebSocket for Android

Release

Overview

This library provide support for STOMP protocol https://stomp.github.io/ At now library works only as client for backend with support STOMP, such as NodeJS (stompjs or other) or Spring Boot (SockJS).

Add library as gradle dependency

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

Example backend (Spring Boot)

WebSocketConfig.groovy

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello")/*.setAllowedOrigins('*')*/.withSockJS();
    }

}

HelloSockController.groovy

@RestController
class HelloSockController {

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    def greeting(String msg) throws Exception {
        println("Receive greeting ${msg}")
        "ECHO: " + msg;
    }
}

Example library usage

Basic usage

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

Method Stomp.over consume class for create connection as first parameter. You must provide dependency for lib and pass class. At now supported connection providers:

  • WebSocket.class ('org.java-websocket:Java-WebSocket:1.3.0')

You can add own connection provider. Just implement interface ConnectionProvider. If you implement new provider, please create pull request :)

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;
    }
});

Library support just send & receive messages. ACK messages, transactions not implemented yet.