rsobies
rsobies

Reputation: 451

spring boot with websocket and postman

I have created test project according to tutorial

@Configuration
@EnableWebSocketMessageBroker
public class WSConfig implements WebSocketMessageBrokerConfigurer {

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

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

@Controller
public class WSController {

    @MessageMapping("/chat")
    @SendTo("/topic/messages")
    public String send(String message) throws Exception {
        return "test";
    }
}

I am struggling to connect with postman, I tried different urls:

each time I click connect button, I get 404 as a response What am I doing wrong? (I am new to websocket)

Upvotes: 8

Views: 12619

Answers (2)

HolzHacker
HolzHacker

Reputation: 33

As I'm struggeling with the same problem and this is the most matching internet post I found, I can confirm connecting to ws://localhost:8080/chat should successfully establish a websocket connection using postman.

But now comes the same problem like @prachit-roarane mentioned in the comments. From the code of the original post we also want to:

  • send messages (client -> server) to /app/chat
  • receive/subscribe messages (server -> client) from /topic/messages

Can you achieve that using postman?

Trying to connect to the following URLs using postman still returns 404:

  • ws://localhost:8080/app/chat
  • ws://localhost:8080/topic/messages

And using postmans send message feature with the successfully established connection at ws://localhost:8080/chat doesn't call

@MessageMapping("/chat")
@SendTo("/topic/messages")
public String send(String message) throws Exception {
    return "test";
}

I'm using this tutorial which uses pretty much the same backend code from original post but implements a working websocket client with angular which we want to replace by postman here.

Upvotes: 1

Asif A Fasih
Asif A Fasih

Reputation: 207

I was able to connect to the ws://localhost:8080/chat endpoint after following instructions at https://blog.postman.com/postman-supports-websocket-apis/

enter image description here

Note: I also looked at https://spring.io/guides/gs/messaging-stomp-websocket/ for more information on setting up a SpringBoot WebSocket project.

Upvotes: 7

Related Questions