> ## Documentation Index
> Fetch the complete documentation index at: https://simplecloud.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# React to server availability

> Run your plugin logic when a server becomes available

Use a server event when your plugin needs to continue work as soon as a server becomes available. For example, a matchmaking or routing system can resume requests that were waiting for a server to start.

## Listen for available servers

The listener uses the `CloudApi` client your plugin already owns and notifies your application code when a server becomes available:

```java ServerAvailabilityListener.java theme={null}
package com.example;

import app.simplecloud.api.CloudApi;
import app.simplecloud.api.event.Subscription;
import app.simplecloud.api.server.ServerState;

public final class ServerAvailabilityListener implements AutoCloseable {
    private final Subscription subscription;

    public ServerAvailabilityListener(
        CloudApi cloudApi,
        Runnable onServerAvailable
    ) {
        subscription = cloudApi.event().server().onStateChanged(event -> {
            if (event.getNewState() == ServerState.AVAILABLE) {
                onServerAvailable.run();
            }
        });
    }

    @Override
    public void close() {
        subscription.close();
    }
}
```

Create the listener when your plugin starts and connect it to the work that is waiting for an available server:

```java theme={null}
serverAvailability = new ServerAvailabilityListener(
    cloudApi,
    () -> matchmaking.refreshAvailableServers()
);
```

Treat the callback as a signal to retry waiting work or refresh the destinations known to your routing system.

<Note>
  Events only report changes that happen after you subscribe. If your plugin also needs the current state when it starts, load it separately through the [Servers API](/docs/en/developer/api/servers).
</Note>

If the callback changes Bukkit, Folia, Velocity, or BungeeCord state, schedule that platform-specific work through the platform's scheduler.

## Close the listener

Close the listener when your plugin stops, before closing the shared `CloudApi` client:

```java theme={null}
if (serverAvailability != null) {
    serverAvailability.close();
}
```

This stops future callbacks. The listener does not close the shared `CloudApi` client.

See the [Events API reference](/docs/en/developer/api/events) for the other event categories and callback types.
