Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring stomp web sockets client for android

Can somebody please let me know how to connect to spring stomp web socket from android client.

WebSocketConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ticket").withSockJS();
    }

}

PushMessageNotifier.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;    

@Service
@EnableAsync
public class PushMessageNotifier {

    private SimpMessagingTemplate simpMessagingTemplate;

    @Autowired
    public PushMessageNotifier(SimpMessagingTemplate simpMessagingTemplate) {
        this.simpMessagingTemplate = simpMessagingTemplate;
    }

    @Async
    public Boolean pushToUI(TicketView ticketView) {
        Boolean result = false;
        if (null != ticketView) {
            this.simpMessagingTemplate.convertAndSend("/pushticket/ticket", ticketView);
            result = true;
        }
        return result;
    }

}

Please tell me how can I connect to this socket from android app? Even I don't have idea which android client I need to use to connect to this socket and topic. Thanks in advance

like image 797
Pratap A.K Avatar asked Oct 19 '22 21:10

Pratap A.K


People also ask

Can I use WebSocket in Android?

You can decide to use any WebSocket-based protocol that supports Android.

Can I use WebSockets for mobile app?

Web sockets are HTML5 compatible and provide backward compatibility with older HTML versions. Therefore, they are supported by all modern web browsers — Google Chrome, Mozilla Firefox, Safari, and others. It is also compatible across platforms: Android, iOS, web apps, and desktop apps.

Is STOMP better than WebSocket?

Most popular messaging brokers support STOMP and STOMP over WebSockets either natively or using plugins. In general JavaScript engines in browsers are not friendly to binary protocols, so using STOMP is a better option because it is a text oriented protocol.

Does Spring boot support WebSocket?

The Spring Framework provides support for using STOMP — a simple, messaging protocol originally created for use in scripting languages with frames inspired by HTTP. STOMP is widely supported and well suited for use over WebSocket and over the web.


1 Answers

Its been ages since the question is asked but it shows up on google and have not received any accepted answer hence...

  • this can be achieved easily using following library https://github.com/NaikSoftware/StompProtocolAndroid
  • Important note: which documentation fails to communicate effectively: In case of SockJS lets assume we have to use URL like http://server-host:8080/stompEndpoint but in android it must be ws://server-host:8080/stompEndpoint/websocket. Where differences are 1) http vs ws 2) Appended /websocket in android variant of URL
like image 106
NotABot Avatar answered Nov 15 '22 07:11

NotABot