Skip to content

An Android library that implements the messaging protocol STOMP that can be used over websockets.

License

Notifications You must be signed in to change notification settings

DadeKuma/DroidSTOMP

Repository files navigation

DroidSTOMP

Build Status

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!

How to install

Gradle

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!

How to use

1. Add internet permission to your Android Manifest.

<uses-permission android:name="android.permission.INTERNET"/>

2. Create a new StompClient and set a callback.

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

3. Connect to your server websocket endpoint. After you connect you can subscribe to server topics!

String serverEndpoint = "ws://10.0.2.2:8080/your_server_endpoint";
String topic = "/app/example";
stompClient.connect(serverEndpoint);
stompClient.subscribe(topic);

4. If you want to send a message you can do it in this way.

String topic = "/app/example";
String message = "hello world!";
stompClient.send(topic, message);

If you need more examples you can check the sample application.

IMPORTANT NOTE:

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.

Backend

If you are using Spring Boot as your backend your classes should look like this:

WebSocketConfig.java

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

HelloWorldController.java

@RestController
public class HelloWorldController {

    @MessageMapping("/example")
    @SendTo("/topic/exampleTopic")
    public String processMessageFromClient(String message){
        return message;
    }
}

About

An Android library that implements the messaging protocol STOMP that can be used over websockets.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages