Event Source API connection abruptly terminates after locking the mobile device

I’m using universal_html package for EventSource for server sent events and reading the charging status for an EV application. This is working fine when the device screen is not locked, but the connection is getting terminated when I lock the device. It’s happening on both Android & iOS platforms. There are no logs once the connection is closed, and it’s hard to debug without any logs.

Any help is much appreciated.

import 'dart:async';
import 'package:universal_html/html.dart';
import 'package:mobx/mobx.dart';

        
class _EventScreenState extends State<EventScreen> {
  
    EventSource eventSource;
        
    @override
    void initState() {
    super.initState();
         
    eventSource = new EventSource(Uri.parse(sseUrl).toString());
    print("getting Eventsource value");
        eventSource.onMessage.listen((event) {
    }, 
    onError: (error){
      print("on message error");
      if(eventSource != null)
      {
          eventSource.close();
          print("event closed");
      }
      
      eventErrorMessage("Connection lost, Please wait for sometime", context);
      this.isListening = false;
      connectSession();
      print(error);}, 
      onDone: (){
            print("done");
        },cancelOnError: true);

      eventSource.onError.listen((event) {
      timerSnackbar?.cancel();
      print(event);
        
     
      if(eventSource != null)
      {
        eventSource.close();
        print("event closed");
      }
        
      // eventErrorMessage(event.toString(), context);
      eventErrorMessage("Connection lost, Please wait for sometime", context);
      this.isListening = false;
      connectSession();

          });
          }
      }
  }