Implement Continuous Network Connectivity Monitoring

Gautier Siclon
Gautier Siclon

Co-founder, Apparence.io

Flutter tips · Published on

When building apps like multiplayer games or chatbots, ensuring a stable connection for your users is super important. You need to know if they can reach your backend server, not just if they have Wi-Fi. This guide shows you how to implement continuous network monitoring in Flutter.

Why Check Beyond Basic Connectivity?

You might think just checking if Wi-Fi or mobile data is on using a package like connectivity_plus is enough. But that only tells you about the device's internet access, not if your specific server is actually reachable or online. For a truly connected experience, you need to check if your app can talk to your backend.

How to Implement Continuous Monitoring

Here’s a simple way to set up a network checker that continuously pings your server.

Defining Network Status

First, we define two states for our network:

sealed class NetworkStatus {}
class NetworkStatusOk extends NetworkStatus {}
class NetworkStatusError extends NetworkStatus {}

These classes help us clearly know if the network is good or not.

The NetworkChecker Class

We create a NetworkChecker class that takes a url (your backend server address) and an interval for how often to check.

class NetworkChecker {
  final String url;
  final Duration interval;

  const NetworkChecker({
    required this.url,
    required this.interval,
  });

  Stream<NetworkStatus> start() async* {
    while (true) {
      final status = await hasConnection();
      yield switch (status) {
        true => NetworkStatusOk(),
        false => NetworkStatusError(),
      };
      await Future.delayed(interval);
    }
  }

  Future<bool> hasConnection() async {
    final client = HttpClient();
    try {
      final res = await client.headUrl(Uri.parse(url));
      return res.statusCode >= 200 && res.statusCode < 300;
    } catch (e) {
      return false;
    } finally {
      client.close();
    }
  }
}

The start() method continuously checks the connection at the set interval and emits NetworkStatusOk or NetworkStatusError.

The Power of a HEAD Request

The hasConnection() method is where the real check happens. It uses an HttpClient to send a HEAD request to your backend URL. A HEAD request is great because it asks for only the response headers. This is super fast and uses less data since the server doesn't send the full response body. If the status code is good (between 200 and 299), we know our server is reachable.

Using the Network Checker in Your App

In your app, you can set up an instance of NetworkChecker and listen for status changes:

void testIt() async {
  const networkChecker = NetworkChecker(
    url: "https://yourbackend.com",
    interval: Duration(seconds: 30),
  );

  networkChecker.start().listen((status) {
    switch (status) {
      case NetworkStatusOk():
        print('Network is up!');
        break;
      case NetworkStatusError():
        // Handle network down: maybe show a message or lock the app.
        print('Network is down');
        break;
    }
  });
}

Remember to use only one instance of NetworkChecker to avoid unnecessary resource usage. You can then update your UI or show friendly messages to users based on the network status.

Save 3 months of work

One command. Pick your modules. Firebase or Supabase auto-configured. Start building what matters.

kickstarter for flutter apps

Frequently Asked Questions

Why is continuous network monitoring better than just checking Wi-Fi?

Checking Wi-Fi only tells you if the device has internet access. Continuous network monitoring, especially by pinging your backend, confirms that your specific server is reachable and online, which is crucial for apps that rely on server communication.

What is a HEAD request and why is it used here?

A `HEAD` request asks a server for only the response headers, not the full content of a webpage or resource. It's faster and uses less bandwidth than a `GET` request because the server doesn't send the body, making it perfect for quick connectivity checks.

How often does the network check run?

The frequency is set by the `interval` duration when you create the `NetworkChecker` instance. In the example, it's set to check every 30 seconds.

What should I do when the network goes down?

When the `NetworkStatusError` is received, you can show a friendly message to the user, disable certain features, or even 'lock' parts of the app that require a server connection until it's restored.

Read more
You may also be interested in
Made by ApparenceKit logo
ApparenceKit is a flutter start kit | template generator tool by Apparence.io © 2026.
All rights reserved