How to receive socket events inside a flutterBackgroundServices method [FLUTTER]

Viewed 68

I am using a server in node js to run socket.io, what i want to do is that when the app is in the background it still receive events from the sockets, can only send events, but does not receive them.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  socketConnection.socket.connect();
  await FlutterBackgroundService.initialize(onStart); //here start executing the onStart method
  runApp(MyApp());
}


onStart() async {
  WidgetsFlutterBinding.ensureInitialized();
  final service = FlutterBackgroundService();
  if (await service.isServiceRunning()) {
    socketEvents();//socket events method
  }
}

socketEvents() {
  log(socketConnection.socket.connected.toString()); //here I want to know if it is connected and it always shows me **false**, but it does connect
  socketConnection.socket.on('noty_event', (data) async {
    log(data['msg'].toString()); //I want to show in the console when an event arrives but it does not show anything
  });
}
0 Answers
Related