Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Geolocation services kill Webscocket connections on Android #1616

Open
3 of 8 tasks
realkalash opened this issue Dec 5, 2024 · 1 comment
Open
3 of 8 tasks

Comments

@realkalash
Copy link

realkalash commented Dec 5, 2024

Please check the following before submitting a new issue.

Please select affected platform(s)

  • Android
  • iOS
  • Linux
  • macOS
  • Web
  • Windows

Steps to reproduce

  1. Prepare/find available server with web-socket support
  2. Open the example app on Android (the issue persist only on Android)
  3. The websocket channel will be connected automatically
  4. Everything will work as expected
  5. Click "Enable geolocator"
  6. Pings (and actual data) from websockets will throttle more and more until completelly muted

Expected results

I expect ping messages each 3 seconds from websocket channel.

[FE][socket] data: {"type":"welcome"}
[FE][socket] data: {"type":"ping","message":1733363850}
[FE][timer] second passed 1
[FE][timer] second passed 2
[FE][timer] second passed 3
[FE][socket] data: {"type":"ping","message":1733363853}
[FE][timer] second passed 4
[FE][timer] second passed 5
[FE][timer] second passed 6
[FE][socket] data: {"type":"ping","message":1733363853}
...

Actual results

As soon as I enable Geolocator (or Google Maps) with accuracy set to "best," it breaks the connection more and more until it is finally completely muted.

[FE][socket] data: {"type":"welcome"}
[FE][timer] second passed 1
[FE][socket] data: {"type":"ping","message":1733363850}
[FE][timer] second passed 2
[FE][timer] second passed 3
[FE][timer] second passed 4
[FE][socket] data: {"type":"ping","message":1733363853}
[FE][timer] second passed 5
[FE][timer] second passed 6
[FE][timer] second passed 7
[FE][socket] data: {"type":"ping","message":1733363856}
[FE][timer] second passed 8
[FE][timer] second passed 9
[FE][timer] second passed 10
[FE][socket] data: {"type":"ping","message":1733363859}
[FE][timer] second passed 11
[FE][timer] second passed 12
[FE][timer] second passed 13
[FE][socket] data: {"type":"ping","message":1733363862}
[FE][timer] second passed 14
[FE][timer] second passed 15
[FE][timer] second passed 16
[FE][socket] data: {"type":"ping","message":1733363865}
[FE][timer] second passed 17
[FE][timer] second passed 18
[FE][timer] second passed 19
[FE][socket] data: {"type":"ping","message":1733363868}
[FE][timer] second passed 20
[FE][timer] second passed 21
[FE][timer] second passed 22
[FE][socket] data: {"type":"ping","message":1733363871}
[FE][timer] second passed 23


E/FlutterGeolocator( 9327): Geolocator position updates started
[FE][timer] second passed 24
[FE][timer] second passed 25
[FE][socket] data: {"type":"ping","message":1733363874}
[FE][timer] second passed 26
[FE][timer] second passed 27
[FE][timer] second passed 28
[FE][geolocator] data: Latitude: 43.658825, Longitude: -79.72306
[FE][socket] data: {"type":"ping","message":1733363877}
[FE][timer] second passed 29
[FE][timer] second passed 30
[FE][timer] second passed 31
[FE][timer] second passed 32
[FE][timer] second passed 33
[FE][geolocator] data: Latitude: 43.658821, Longitude: -79.7230634
[FE][socket] data: {"type":"ping","message":1733363880}
[FE][timer] second passed 34
[FE][timer] second passed 35
[FE][timer] second passed 36
[FE][timer] second passed 37
[FE][socket] data: {"type":"ping","message":1733363883}
[FE][socket] data: {"type":"ping","message":1733363886}
[FE][timer] second passed 38
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 39
[FE][timer] second passed 40
[FE][timer] second passed 41
[FE][timer] second passed 42
[FE][timer] second passed 43
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 44
[FE][timer] second passed 45
[FE][timer] second passed 46
[FE][timer] second passed 47
[FE][timer] second passed 48
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 49
[FE][timer] second passed 50
[FE][timer] second passed 51
[FE][timer] second passed 52
[FE][timer] second passed 53
[FE][geolocator] data: Latitude: 43.6588209, Longitude: -79.7230634
[FE][timer] second passed 54
[FE][timer] second passed 55
[FE][timer] second passed 56
[FE][timer] second passed 57

Code sample

Code sample
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(TestSocketsApp());
}

class TestSocketsApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'WebSocket App',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    subscribeToFEwebsocket(context);
  }

  Future<void> subscribeToFEwebsocket(BuildContext context) async {
    try {
      final headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'Accept-Language': 'en-CA,en-US;q=0.7,en;q=0.3',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive, Upgrade',
        'Host': '*',
        'Origin': 'https://*',
        'Pragma': 'no-cache',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'websocket',
        'Sec-Fetch-Site': 'same-site',
        'Sec-WebSocket-Extensions': 'permessage-deflate',
        'Sec-WebSocket-Key': '*',
        'Sec-WebSocket-Protocol': 'actioncable-v1-json, actioncable-unsupported',
        'Sec-WebSocket-Version': '13',
      };
      const sid = '*'; // Placeholder for sid
      const authToken = '*'; // Placeholder for authToken
      const socketUrl =
          'wss://*.org/cable?sid=$sid&auth_token=$authToken';

      final socket = await WebSocket.connect(
        socketUrl,
        headers: headers,
      );
      Timer.periodic(const Duration(seconds: 1), (timer) {
        print('[FE][timer] second passed ${timer.tick}');
      });
      socket.listen(
        (data) {
          print('[FE][socket] data: $data');
        },
        onError: (error) {
          print('[FE][socket] error: $error');
        },
        onDone: () {
          print('[FE][socket] connection closed');
        },
      );
    } catch (e) {
      print('[FE][socket] error: $e');
    }
  }

  StreamSubscription<Position>? listener;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            FilledButton(
              onPressed: () async {
                await enablePermissions();
                listener?.cancel();
                listener = Geolocator.getPositionStream(
                  locationSettings: const LocationSettings(
                    // if I use LocationAccuracy.best, the ping messages will throttle
                    accuracy: LocationAccuracy.best,
                  ),
                ).listen((data) {
                  print('[FE][geolocator] data: $data');
                });
              },
              child: const Text('Enable geolocator (ping messages will throttle)'),
            ),
          ],
        ),
      ),
    );
  }
  
  Future<void> enablePermissions() async {
    final permission = await Geolocator.checkPermission();
    if (permission != LocationPermission.whileInUse) {
      await Geolocator.requestPermission();
    }
  }
}

Screenshots or video

Screenshots or video demonstration

Not needed

Version

All versions

Flutter Doctor output

Doctor output
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.5, on macOS 15.1.1 24B91 darwin-arm64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.95.3)
[✓] Connected device (5 available)
    ! Error: Browsing on the local area network for Alex’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!
@realkalash
Copy link
Author

It may not be related to the Geolocator itself because I'm seeing the same results with Google Maps when I enable "show my position on the map". But if someone can explain to me why it's happening or can somehow help me to resolve this issue, it would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant