Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4312

Using MessageMapping with PathVariable (Kotlin)

Is it even possible in Spring Boot (STOMP Websockets) to send a message with a particular id ?

When I do this

@MessageMapping("/room")
@SendTo("/topic/room") 
fun handleRoomMessage1(message: ChatMessagePayload): ChatMessage {
    // Add any processing logic here if needed (e.g., validate or enrich the message)
    val timestamp = Instant.now().atZone(ZoneId.systemDefault()).toEpochSecond()
    val message = ChatMessage(
        id = UUID.randomUUID().toString(),
        content = message.ahmed,
        timestamp = timestamp,
        ownerImageUrl = "imageUrl",
        ownerId = message.ownerId.toInt()
    )
    return message // This will be broadcast to all subscribers
}

It works fine and I can see in the logs that the message was received at the server end

However this always fails

@MessageMapping("/room/{id}") // Clients send to /app/room/{id}
@SendTo("/topic/room/{id}") // Broadcasts to all subscribers of /topic/room/{id}
fun handleRoomMessage(
    @PathVariable id: String,
    @Payload message: ChatMessagePayload
): ChatMessage {
    // Add any processing logic here if needed (e.g., validate or enrich the message)

    val timestamp = Instant.now().atZone(ZoneId.systemDefault()).toEpochSecond()
    val message = ChatMessage(
        id = UUID.randomUUID().toString(),
        content = message.ahmed,
        timestamp = timestamp,
        ownerImageUrl = "imageUrl",
        ownerId = message.ownerId.toInt()
    )
    return message // This will be broadcast to all subscribers
}

In the logs I can see that jackson is trying to deserialize the message that I have sent with the path variable .

    Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (byte[])"{"ahmed":"bbbb","ownerId":3}"; line: 1, column: 1]

For additional context I am using a library called krossbow to send messages.

stompSession?.convertAndSend("/app/room/10", message)

Upvotes: 0

Views: 8

Answers (0)

Related Questions