2

I have been following this for reference. I am developing a spring-boot app which will have authenticated users. Once logged in, a user will subscribe to an event by visiting a specific URL.

This spring-boot app will also either support MQTT (or maybe just HTTP requests) in which information for a specific user will be sent. I would like to then display this sent information to the user using web flux/SSE if the user has subscribed.

Many users can be logged in at any given time, and they will have all subscribed to the updates. How do I manage all the different sinks for each logged in user?

I believe it's possible to get the current user when they visit the authenticated URL, but what's a method of storing all of the sinks for each logged in user?

I appreciate it.

bizzysven
  • 21
  • 2
  • 1
    You can use one sink, and just subscribe to it multiple times for each user. – 123 Apr 12 '20 at 15:40
  • When I receive data for a user, it is only intended for a user. When I would call sink.next to push the data, how could I specify which user is getting it? Many users will be subscribed. – bizzysven Apr 12 '20 at 15:59
  • You can use a filter, put your conditions in that – 123 Apr 12 '20 at 16:03
  • I'm trying to follow. I understand I can use the Java 8 filter, but what would I be filtering? The MQTT or HTTP data I get will be to a specific topic/id, so I can figure out which user it belong to. But, what parameter can I add to the filter so I know the message is going to the correct user? – bizzysven Apr 12 '20 at 16:59

1 Answers1

3

You already got the answer in the comment section.

Lets assume that this is the message format you would be publishing.

public class Message {

    private int intendedUserId;
    private String message;

    // getters and setters

}

Just have 1 processor and sink from the processor.

 FluxProcessor<Message> processor;
 FluxSink<Message> sink;

Push all the messages via the sink.

sink.next(msg);

Your controller would be more or less like this. Here I assume you have some method to get the user id authtoken.getUserId(). Here the filter is part of the Flux.

@GetMapping(value = "/msg", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Message> getMessages(){
    return processer
                .filter(msg -> msg.getIntendedUserId() == authtoken.getUserId());
}
vins
  • 15,030
  • 3
  • 36
  • 47