SwiftStomp
SwiftStomp copied to clipboard
User specific messaging does not work
I'm facing issues establishing a STOMP connection between my Swift client and Spring Boot. The problem arises when I try to send a message to a specific user — Spring successfully sends the message, but it doesn’t reach the Swift client. However, when I send a message from Spring to a general topic, it works perfectly. My goal is to be able to send messages to individual users.
Swift
class MultiplayerViewModel: ObservableObject {
private var socketClient: SwiftStomp?
let socketURL = URL(string: "ws://localhost:8091/ws")!
func connect() {
self.socketClient = SwiftStomp(host: socketURL)
self.socketClient?.delegate = self
self.socketClient?.autoReconnect = true
socketClient?.connect()
}
func disconnect() {
socketClient?.disconnect()
}
}
extension MultiplayerViewModel: SwiftStompDelegate {
func onConnect(swiftStomp: SwiftStomp, connectType: StompConnectType) {
print("Stomp Client Connected: \(connectType)")
socketClient?.subscribe(to: "/user/queue/greetings", mode: .clientIndividual)
socketClient?.send(body: "lol", to: "/app/greetings")
}
func onDisconnect(swiftStomp: SwiftStomp, disconnectType: StompDisconnectType) {
print("Stomp Client Disconnected: \(disconnectType)")
}
func onError(swiftStomp : SwiftStomp, briefDescription : String, fullDescription : String?, receiptId : String?, type : StompErrorType) {
print("Error: \(briefDescription)")
}
func onMessageReceived(swiftStomp: SwiftStomp, message: Any?, messageId: String, destination: String, headers : [String : String]) {
print("Received message from destination: \(message)")
}
func onReceipt(swiftStomp : SwiftStomp, receiptId : String) {
}
}
Spring Boot
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic", "/user");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOriginPatterns("*");
}
}
@Controller
public class StompController {
@MessageMapping("/greetings")
@SendToUser("/queue/greetings")
public String reply(@Payload String message) {
return "Hello " + message;
}
}
Logs
2024-09-15T19:34:51.321+03:00 DEBUG 67961 --- [nio-8091-exec-5] o.s.w.s.s.s.WebSocketHttpRequestHandler : GET /ws
2024-09-15T19:34:51.439+03:00 DEBUG 67961 --- [nio-8091-exec-5] s.w.s.h.LoggingWebSocketHandlerDecorator : New StandardWebSocketSession[id=43304528-2cb0-e7c3-cfb5-12f770d36b71, uri=ws://localhost:8091/ws]
2024-09-15T19:34:55.620+03:00 DEBUG 67961 --- [nboundChannel-2] o.s.m.s.b.SimpleBrokerMessageHandler : Processing CONNECT session=43304528-2cb0-e7c3-cfb5-12f770d36b71
2024-09-15T19:34:56.412+03:00 DEBUG 67961 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler : Processing SUBSCRIBE /user/queue/greetings id=/user/queue/greetings session=43304528-2cb0-e7c3-cfb5-12f770d36b71
2024-09-15T19:34:56.416+03:00 DEBUG 67961 --- [nboundChannel-7] .WebSocketAnnotationMethodMessageHandler : Searching methods to handle SEND /app/greetings session=43304528-2cb0-e7c3-cfb5-12f770d36b71 text/plain payload=lol, lookupDestination='/greetings'
2024-09-15T19:34:56.421+03:00 DEBUG 67961 --- [nboundChannel-7] .WebSocketAnnotationMethodMessageHandler : Invoking StompController#reply[2 args]
2024-09-15T19:34:56.427+03:00 DEBUG 67961 --- [nboundChannel-7] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/user/43304528-2cb0-e7c3-cfb5-12f770d36b71/queue/greetings session=43304528-2cb0-e7c3-cfb5-12f770d36b71 payload=Hello lol