Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

346
Views
How to connect Spring Boot STOMP with SockJS

Server:

// SocketBrokerConfig.java

@Configuration
@EnableWebSocketMessageBroker
class SocketBrokerConfig : WebSocketMessageBrokerConfigurer {

    override fun configureMessageBroker(registry: MessageBrokerRegistry) {
        registry.enableSimpleBroker(
            LoginController.RESPONSE,
        )
        registry.setApplicationDestinationPrefixes("/server")
        registry.setUserDestinationPrefix("/player")
    }

    override fun registerStompEndpoints(registry: StompEndpointRegistry) {
        registry.addEndpoint(LoginController.URI).setAllowedOrigins("http://localhost:3000").withSockJS()
    }
}


@Controller
class LoginController(
    private val msgTemplate: SimpMessagingTemplate,
) {

    @MessageMapping("/server/login")
    fun handle(
        @Payload request: LoginRequest,
        @Header("simpSessionId") sessionId: String,
    ) {
        println("LoginController : $sessionId")
        msgTemplate.convertAndSend("/player/login", LoginResponse("TEST"))
    }

}

data class LoginRequest(
    val username: String,
)

data class LoginResponse(
    val token: String,
)

Browser lient:

import SockJS from "sockjs-client";
import {Stomp} from "@stomp/stompjs";


      const socket = new SockJS('http://localhost:8080/server/login');
      const stompClient = Stomp.over(socket);
      stompClient.connect({}, () => {
          let url = stompClient.ws._transport.url;
          console.log({url});
          url = url.replace(
              "ws://localhost:8080/server/login/",  "");
          url = url.replace("/websocket", "");
          url = url.replace(/^[0-9]+\//, "");
          console.log("Your current session is: " + url);

          stompClient.subscribe('login', (msg) => {
              console.log({msg});
          })
          stompClient.send('/server/login', {}, JSON.stringify({ username: 'TEST' }))
          stompClient.send('server/login', {}, JSON.stringify({ username: 'TEST' }))
          stompClient.send('/login', {}, JSON.stringify({ username: 'TEST' }))
          stompClient.send('login', {}, JSON.stringify({ username: 'TEST' }))
      })

And the console output: enter image description here

But there is not response from server, and also nothing in server console (there is println in controller)

It's my first time with Stomp, Spring Boot Websockets and SockJS I understand how TCP works, but I don't understand what rule I'm breaking here that this is not working :(

Please help

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!