Skip to content

Commit

Permalink
introducing websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelebarre committed Aug 21, 2024
1 parent 7e9550f commit 71f6c1f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.delebarre.bookappbackend.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

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

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

Expand All @@ -19,6 +23,8 @@ public class BookController {

private final BookService bookService;

private final SimpMessagingTemplate messagingTemplate;

@CrossOrigin(origins = "*")
@GetMapping("/all")
public List<Book> getAllBooks() {
Expand All @@ -40,6 +46,7 @@ public ResponseEntity<?> createBook(
try {
Book book;
book = bookService.createBook(olid);
messagingTemplate.convertAndSend("/topic/books", "update");

return ResponseEntity.status(HttpStatus.CREATED).body(book);
} catch (BookAlreadyExistsException e) {
Expand All @@ -53,13 +60,16 @@ public ResponseEntity<?> createBook(
@PutMapping
public ResponseEntity<Book> updateBook(@RequestParam String id, @RequestBody Book book) {
Book updatedBook = bookService.updateBook(id, book);
messagingTemplate.convertAndSend("/topic/books", "update");
return ResponseEntity.ok(updatedBook);
}

@CrossOrigin(origins = "*")
@DeleteMapping
public ResponseEntity<?> deleteBook(@RequestParam String id) {
return bookService.deleteBook(id);
bookService.deleteBook(id);
messagingTemplate.convertAndSend("/topic/books", "update");
return ResponseEntity.ok().build();
}

@CrossOrigin(origins = "*")
Expand Down

0 comments on commit 71f6c1f

Please sign in to comment.