0

I deal with the topic of reactive programming. I use WebFlux and R2dbc to access the database. I am developing a small backend for a chat app. So I have some questions on the subject and I would be very happy about suggestions and improvements.

This is service class

@Service
public class MessageService implements IMessageService {

    UnicastProcessor<Message> hotProcessor = UnicastProcessor.create();
    private final FluxSink<Message> fluxSink = hotProcessor.sink(FluxSink.OverflowStrategy.LATEST);
    private final Flux<Message> hotFlux = hotProcessor.publish().autoConnect();



    @Autowired
    private MessageRepository messageRepository;

    public Flux<Message> findAll() {
        return this.messageRepository.findAll();
    }


    public Mono<Message> create(Message nachricht) {
        this.fluxSink.next(nachricht);
        return this.messageRepository.save(nachricht);
    }

    // how can I persist messages and to arrange a chat room.
    public Flux<Message> finAllMessagesByChatroomId(Long id) {
        return hotFlux.filter(m->m.getId().intValue() == id.intValue());        
    }
}

When should I use UnicastProcessor when DirectProcessor.

Update:
a little correction

public Flux<Message> finAllMessagesByChatroomId(Long id) {
        return hotFlux.filter(m->m.getRoom_id == id);        
}

But how can I get all the Messages from the database and add it to fluxSink?

  • I see three different questions in the body and the title of your text. Please clarify which you want to be answered and what the exact problem is. – Jens Schauder Mar 26 '19 at 06:31
  • Thank you for your reply. I want to save data in database and if anyone is in the chat room, then he should get messages. I'm very unsure what to do exactly. –  Mar 26 '19 at 10:31

0 Answers0