'Do a connection using EventSource from Android with HTTPS to Spring-Boot server (JAVA)

I have a spring-boot server which only accepts https requests, the point is that I need it to be able to communicate with the client (android) using SSE.

I have configured the server so that they can subscribe and receive events. Tested with the browser it works perfectly. But I can't find any implementation that will let me connect to the server with https.

This is the code in my Spring-Boot Controller:

public class NewsController {

    
    public List<SseEmitter> emitters=new CopyOnWriteArrayList<>();

    // method for client subscription
    @CrossOrigin
    @RequestMapping(value = "/subscribe", consumes=MediaType.ALL_VALUE)
    public SseEmitter subscribe(){
        SseEmitter sseEmitter=new SseEmitter(Long.MAX_VALUE);
        try{
            sseEmitter.send(SseEmitter.event().name("INIT"));
        }catch (IOException e){
            e.printStackTrace();
        }
        sseEmitter.onCompletion(() -> emitters.remove(sseEmitter));
        
        emitters.add(sseEmitter);
        return sseEmitter;
    }

    // method for dispatching events to all clients
    @PostMapping(value="/dispatchEvent")
    public String dispatchEventToClients (@RequestParam String dataEvent){
        for(SseEmitter emitter:emitters){
            try{
                emitter.send(SseEmitter.event (). name("Event").data(dataEvent));
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return "WORKS";
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source