1

I'm trying to redirect my Angular web application to a different URL that receives from server-sent events. This feature works well when the browser is in the foreground in IOS. However, when a user changes the browser to the background, the Angular application does not listen to any events.

This issue only happens in IOS. In Andriod, browsers receive server-sent events even when the browser is in the background.

service.ts

getServerSentEvent(merchantTransactionId: string, txnDate: string): Observable<any> {
    return new Observable<string>(obs => {
      const appType = 'guest-checkout';
      const url = environment.BASE_DATA_URL + '/api/event/server-sent/event/subscribe/' + appType + '/' + merchantTransactionId;
      const es = new EventSource(url);
      es.addEventListener('message', (event) => {
          // alert(event.data);
          obs.next(event.data);
      });
      return () => es.close();
    });
  }

I have added an alert box to test whether it appears on mobile or not. It does not appear when the browser is in the background. Also, I have tried onmessage instead of addEventListener and there was no change.

Component.ts

triggerSSEEvent(merchantTxnId, txnDateString): void {
    this.sseService.getServerSentEvent(merchantTxnId, txnDateString).subscribe(
      (data) => {
        const response = JSON.parse(data);
        this.responseURL = response.url;
        window.location.href = response.url;
      },
      (error) => {
        this.showError();
      }
    );
  }

Note: The user usually switches the browser in order to accept the push request from another app.

ONE_FE
  • 968
  • 3
  • 19
  • 39
  • 1
    Remember that Chrome on IOS is actually just Safari, under the hood. My guess is that this is a battery-saving feature, and not something you can code around; you might be able to configure it in the (advanced) browser settings? – Darren Cook Jan 27 '22 at 18:10
  • @DarrenCook We observed that the backend does not send the link to the frontend as the connection is paused when the browser is in the background. This needs to be fixed from the backend as per the current observations. Will let you know once we find a fix to this issue. – ONE_FE Jan 29 '22 at 16:47
  • 1
    Safari mobile pauses almost everything when in background. – Luis Rivera Apr 18 '22 at 09:22
  • 1
    I'm not sure which events (if any) is being triggered before and after background. This question mentions a few https://stackoverflow.com/questions/4656387/how-to-detect-in-ios-webapp-when-switching-back-to-safari-from-background You might try to listen for that event to close and reconnect EventSource. If you are trying to use this for things like push notifications while in background it wont work. – Luis Rivera Apr 18 '22 at 09:44

0 Answers0