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
You can decide to use any WebSocket-based protocol that supports Android.
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.
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.
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.
Its been ages since the question is asked but it shows up on google and have not received any accepted answer hence...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With