[Vuejs]-How get response in axios when api is steam like SSE (Server-Send Events)

0👍

My solution chose the fetch-event-source framework.

https://github.com/Azure/fetch-event-source

Here are some other alternatives:

JavaScript+axios:

axios({
  url: 'https://api',
  data: {
    prompt: 'json data'
  },
  headers: {
    'accept': '*',
    'content-type': 'application/json'
  },
  method: 'POST',
  onDownloadProgress: progressEvent => {
     const xhr = progressEvent.event.target
     const { responseText } = xhr
     console.log("=====responseText======")
     console.log(responseText)
  }
}).then(({ data }) => Promise.resolve(data));

Server+Java code:

spring-server-sent-events: https://www.baeldung.com/spring-server-sent-events

 public void consumeServerSentEvent() {
        WebClient client = WebClient.create("http://localhost:8080/sse-server");
        ParameterizedTypeReference<ServerSentEvent<String>> type
         = new ParameterizedTypeReference<ServerSentEvent<String>>() {};
    
        Flux<ServerSentEvent<String>> eventStream = client.get()
          .uri("/stream-sse")
          .retrieve()
          .bodyToFlux(type);
    
        eventStream.subscribe(
          content -> logger.info("Time: {} - event: name[{}], id [{}], content[{}] ",
            LocalTime.now(), content.event(), content.id(), content.data()),
          error -> logger.error("Error receiving SSE: {}", error),
          () -> logger.info("Completed!!!"));
    }

Leave a comment