Spring Webflux send event when any new data

Viewed 1024

I'm trying to learn Spring webflux & R2DBC. The one I try is simple use case:

  1. have a book table
  2. create an API (/books) that provides text stream and returning Flux<Book>
  3. I'm hoping when I hit /books once, keep my browser open, and any new data inserted to book table, it will send the new data to browser.

Scenario 2, still from book table:

  1. have a book table
  2. create an API (/books/count) that returning count of data in book as Mono<Long>
  3. I'm hoping when I hit /books/count once, keep my browser open, and any new data inserted /deleted to book table, it will send the new count to browser.

But it does not works. After I isnsert new data, no data sent to any of my endpoint.
I need to hit /books or /books/count to get the updated data.
I think to do this, I need to use Server Sent Events? But how to do this in and also querying data? Most sample I got is simple SSE that sends string every certain interval.

Any sample to do this?

Here is my BookApi.java

@RestController
@RequestMapping(value = "/books")
public class BookApi {

    private final BookRepository bookRepository;

    public BookApi(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    @GetMapping(value = "/count", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Mono<Long> count() {
        return bookRepository.count();
    }
}

BookRepository.java (R2DBC)

import org.springframework.data.r2dbc.repository.R2dbcRepository;

public interface BookRepository extends R2dbcRepository<Book, Long> {
}

Book.java

@Table("book")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {

    @Id
    private Long id;

    @Column(value = "name")
    private String name;

    @Column(value = "author")
    private String author;

}
1 Answers

Use a Processor or Sink to handle the Book created event.

Check my example using reactor Sinks, and read this article for the details.

Or use a tailable Mongo document.

A tailable MongoDB document can do the work automatically, check the main branch of the same repos.

My above example used the WebSocket protocol, it is easy to switch to SSE, RSocket.

Related