Can I build a real-time app chat with Spring Boot and Websocket?

Hi everyone! Is it possible to build a real-time app chat with Spring Boot and Websocket?

Hello and yes you can,

There’s even a short project on Spring’s docs to try it out:

1 Like

Hey, you can use Websocket to share private messages. Here’s how to make it happen in your Java project.

To get started, you need two dependencies in your pom.xml file: spring-boot-starter-web and spring-boot-starter-websocket. These are enough to effortlessly receive messages from any corner of the world.

@Configuration

@EnableWebSocketMessageBroker

public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer{ @Override

public void registerStompEndpoints(StompEndpointRegistry registry) {

registry.addEndpoint("/socket")

.setAllowedOrigins("*")

.withSockJS();

}

@Override

public void configureMessageBroker(MessageBrokerRegistry registry) {

registry.setApplicationDestinationPrefixes("/app")

.enableSimpleBroker("/chat");

}

}

Here you could find all this procedure step by step. Just take a look.

1 Like

@DanielHuebschmann @YuriSol Great, thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.