├── .gitignore ├── README.md ├── pom.xml └── src └── main ├── java └── pac │ ├── Application.java │ ├── configuration │ └── WebSocketConfiguration.java │ └── controllers │ └── WebSocketController.java └── resources └── application.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | target/ 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot with WebSockets 2 | ## You can get detailed explanation and step by step guide on Medium. 3 | ## [View tutorial on Medium](https://medium.com/oril/spring-boot-websockets-angular-5-f2f4b1c14cee) 4 | ### This app based on Spring boot starter web project that uses WebSockets to make a real time client-server communication. 5 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | groupId 8 | spring-boot-websockets 9 | 1.0-SNAPSHOT 10 | 11 | 1.8 12 | 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.8.RELEASE 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 1.5.2.RELEASE 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-websocket 29 | 1.5.2.RELEASE 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/pac/Application.java: -------------------------------------------------------------------------------- 1 | package pac; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | public static void main(String[] args) { 9 | SpringApplication.run(Application.class); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/pac/configuration/WebSocketConfiguration.java: -------------------------------------------------------------------------------- 1 | package pac.configuration; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.messaging.simp.config.MessageBrokerRegistry; 5 | import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; 6 | import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; 7 | import org.springframework.web.socket.config.annotation.StompEndpointRegistry; 8 | 9 | @Configuration 10 | @EnableWebSocketMessageBroker 11 | public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer{ 12 | @Override 13 | public void registerStompEndpoints(StompEndpointRegistry registry) { 14 | registry.addEndpoint("/socket") 15 | .setAllowedOrigins("*") 16 | .withSockJS(); 17 | } 18 | 19 | @Override 20 | public void configureMessageBroker(MessageBrokerRegistry registry) { 21 | registry.setApplicationDestinationPrefixes("/app") 22 | .enableSimpleBroker("/chat"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/pac/controllers/WebSocketController.java: -------------------------------------------------------------------------------- 1 | package pac.controllers; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.messaging.handler.annotation.MessageMapping; 5 | import org.springframework.messaging.simp.SimpMessagingTemplate; 6 | import org.springframework.stereotype.Controller; 7 | 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | 11 | @Controller 12 | public class WebSocketController { 13 | 14 | private final SimpMessagingTemplate template; 15 | 16 | @Autowired 17 | WebSocketController(SimpMessagingTemplate template){ 18 | this.template = template; 19 | } 20 | 21 | @MessageMapping("/send/message") 22 | public void onReceivedMesage(String message){ 23 | this.template.convertAndSend("/chat", new SimpleDateFormat("HH:mm:ss").format(new Date())+"- "+message); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8888 --------------------------------------------------------------------------------