Search code examples
flutterhttpwebdesktopmdns

Flutter HTTP client, difference desktop / web


I'm currently using the Flutter HTTP package to handle various requests to an ESP32 running a webserver. The device is found by mDNS lookup in my local network. The application using the package should mainly be running on web, but I'd also like to support desktop, at least during development. For some reason though I'm having troubles establishing low latency connections on my desktop platform (Linux).

When running the web build the very first HTTP request usually takes a longer (~5-10s) but every consecutive request can be handled within milliseconds. It then stays that way, even after a longer period of inactivity.

When running the desktop build however a couple of seconds of inactivity result in the next request taking a very long time again.

Here's the snippet I'm using to time the requests. (client is a class member)

    ElevatedButton(
      onPressed: () async {
        print("Start stopwatch");
        final stopwatch = Stopwatch()..start();
        var response =
            await _client.get(Uri.http('MYDOMAIN.local', 'sys/'));
        stopwatch.stop();
        print(
            'Stop stopwatch, elapsed ms ${stopwatch.elapsedMilliseconds}');
        print('Response status: ${response.statusCode}');
        print('Response body: ${response.body}');
      },
      child: const Text('http.Client() request'),
    ),

Naturally both platforms will have vastly different implementations so I know that comparing the two is difficult. I still wonder if I could somehow achieve the web behavior in a desktop build...

/edit
Apparently this is a mDNS resolution issue and not related to Flutter at all.
https://askubuntu.com/questions/1279792/local-hostname-resolution-is-slow-on-20-04


Solution

  • For future reference, the issue has been related to the default nsswitch configuration on my Arch installation. What finally helped me out was the Arch wiki on Avahi, specially the hostname resolution paragraph. In the notes it says that disabling IPv6 can help resolving slowdowns and in my case it did.