An Android library that implements the messaging protocol STOMP that can be used over websockets.
Here's a short demo of the library used to build a simple chat!
Add jitpack repository and DroidSTOMP dependency to your build.gradle
repositories {
...
maven { url "https://jitpack.io" }
}
dependencies {
...
implementation 'com.github.DadeKuma:DroidSTOMP:0.4.1'
}
Done!
<uses-permission android:name="android.permission.INTERNET"/>
stompClient = new StompClient();
stompClient.setStompCallback(new StompCallback() {
@Override
public void onMessageReceived(String topic, Long subscriptionId, String message) {
//in this example we just log the message received...
Log.i("StompExample", "message recevied: " + message + " on topic: " + topic)
}
});
String serverEndpoint = "ws://10.0.2.2:8080/your_server_endpoint";
String topic = "/app/example";
stompClient.connect(serverEndpoint);
stompClient.subscribe(topic);
String topic = "/app/example";
String message = "hello world!";
stompClient.send(topic, message);
If you need more examples you can check the sample application.
Since Android 9.0 (API level 28) cleartext support is disabled by default. So your app will not send cleartext http requests, if you don't enable it.
If you don't know how to solve this problem I suggest to look at this post on StackOverflow.
If you are using Spring Boot as your backend your classes should look like this:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/your_server_endpoint");
}
}
@RestController
public class HelloWorldController {
@MessageMapping("/example")
@SendTo("/topic/exampleTopic")
public String processMessageFromClient(String message){
return message;
}
}