yonoss opened a new issue, #500:
URL: https://github.com/apache/cordova/issues/500

   In Google Chrome for example, EventSource and/or fetch are getting the 
packages from a SseEmitter endpoint, one by one, so that I can get the ChatGPT 
tokens generation effect.
   
   But in my Cordova app, in both Android and iOS, the tokens generation effect 
is not possible, as EventSource and fetch calls, are buffering the backend 
response, delivering the text to the javascript code in a single push, after 
the server stopped emitting.
   
   Any idea why this behavior in Cordova? Is it possible to get the same tokens 
generation effect in Cordova without using a websocket connection?
   
   JS code for EventSource approach:
   ```
   function handleGptSuggestCallEventSource(params, target) {
       let eventSource = mwSuggestEventSource(params)
       
        eventSource.onmessage = function(event) {
            if (event.data.includes(FINAL_MESSAGE)) {
               eventSource.close();  // Stop the EventSource
               return;
           } else {
               let formattedText = event.data.replace(/\\n/g, '<br>');
               formattedText = formattedText.replace(/\n/g, '<br>');
               formattedText = formattedText.replace(/&nbsp;/g, ' ');
   
               updateTargetMessage(formattedText, target);
           }
       };
   
       eventSource.onerror = function(err) {
           eventSource.close();
       };
       
   }
   
   function mwSuggestEventSource(params) {
       return callGPTEventSource(GPT_SUGGEST_URL.format(params));
   }
   
   
   function  callGPTEventSource(url) {
       try {
           return new EventSource(getFullURL(url));
       } catch (e) {
           showToastError(getStr(e));
       }
   }
   ```
   
   Backed java code:
   ```
   public SseEmitter callApiAsync(HttpRequest request) {
           SseEmitter emitter = new SseEmitter();
   
           executorService.execute(() -> {
               try {
                   
                   client.sendAsync(request, 
HttpResponse.BodyHandlers.ofInputStream())
                   .thenAccept(response -> {
                       try (InputStream inputStream = response.body()) {
                           BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream));
                           String line;
                           Matcher matcher;
                           while ((line = reader.readLine()) != null) {
                               matcher = GPT_TEXT_PATTERN.matcher(line);
                               if (matcher.find()) {
                                   line = matcher.group(1);
                               } else {
                                   line = "";
                               }
                               line = line.replace(" ", EMPTRY_SPACE);
                               emitter.send(SseEmitter.event().data(line)); // 
Stream each line to client
                           }
                           emitter.send(FINAL_MESSAGE);
                           emitter.complete(); // Complete the emitter after 
finishing reading
                       } catch (IOException e) {
                           emitter.completeWithError(e);
                       }
                   });
               } catch (Exception e) {
                   emitter.completeWithError(e);
               }
           });
   
           return emitter;
       }
   ```
   cordova --version
   12.0.0 (cordova-lib@12.0.1)
   
   cordova plugin list
   cordova-plugin-android-permissions 1.1.5 "Permissions"
   cordova-plugin-geolocation 4.1.0 "Geolocation"
   cordova-plugin-inappbrowser 6.0.0 "InAppBrowser"
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to