Skip to main content

polling

In the lobby's minigame menu, the Bed Wars icon says "128 playing". The count lives in Redis and is updated by each game server.

The lobby hears nothing from those servers. Querying once with async freezes the count at the moment the menu opened. Running your own timer means remembering to stop it when nobody is looking, or you keep hitting Redis for nothing.

What Signal.polling doesโ€‹

Signal.polling is a Signal.async that queries again periodically. While someone is subscribed, it runs the query at a fixed interval; once the last subscriber leaves, it stops automatically.

The first three arguments match Signal.async, and the fourth is the polling period. Placeholders, error handling, and extra-query rules are the same as for async.

Polling the player countโ€‹

Suppose network.onlineCount(String) reads a game's player count from Redis, and ioExecutor is a background I/O executor. Every player opening the minigame menu can share this source, so create it once when the plugin starts.

Step 1 / 5

The first query is submitted on creation. This only creates the source; without a subscriber it does not keep polling.

AsyncSignal<Integer> bedwarsOnline = Signal.polling(
(Integer) null,
ioExecutor,
() -> network.onlineCount("bedwars"),
100L // query every 100 ticks
);
Signal<String> onlineText = bedwarsOnline.map(value ->
value == null ? "Fetching player countโ€ฆ" : value + " playing"
);

// a player opens the minigame menu, which subscribes to onlineText; polling starts
Subscription watching = onlineText.onDirty(() -> {
System.out.println(onlineText.get());
});
// from here on it queries every 100 ticks and prints only when the count changes

// the last viewer closes the menu; polling stops
watching.close();
Minigames
Bed Wars
timetick 0
pollingnot started
bedwarsOnlinenull
onlineTextFetching player countโ€ฆ
(nothing printed yet)

Menus that bind through dependsOn and similar APIs subscribe on open and unsubscribe on close, so nothing needs to be managed by hand. As long as one menu is open, polling continues.

Polling resumes when someone subscribes again. If more than one period has passed since the last query and none is running, a query runs immediately, so players do not see a stale count.

Polling by real timeโ€‹

100 ticks is about 5 seconds at 20 TPS, longer when the server lags. To poll by real time, use pollingMillis.

AsyncSignal<Integer> bedwarsOnline = Signal.pollingMillis(
(Integer) null,
ioExecutor,
() -> network.onlineCount("bedwars"),
5000L
);

Caveatsโ€‹

No subscriber, no continuous polling

Calling get() or map alone does not start polling; a subscription is required. Stopping does not cancel queries already submitted; they still finish and update the value. Calling bedwarsOnline.dirty() refreshes regardless of subscribers.

Periods and extra queries

Pollers with the same period share one clock and query on its beat, not "5 seconds after the last query finished". When a query takes longer than the period, the same Signal never queries in parallel and instead merges into one extra query.

The polling period must be positive, and pollingMillis must be at least 50 milliseconds.

You read the latest result

A polled value is only the result of the latest query. Whether a game still has room when a player joins must be checked by the game logic in real time.

Writing a Signal.polling for every game gets tedious. A KeyedSignal.polling partitioned by game id only polls the games someone is looking at.

NextListSignalHold a list that grows and shrinks, such as a minigame room's waiting queue.