Giter Club home page Giter Club logo

droidstomp's Introduction

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

droidstomp's People

Contributors

dadekuma avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.