Skip to main content

Live updates (WebSockets)

The Java SDK can listen for real-time updates so new definitions are applied quickly.

Enable​

enableLiveUpdates defaults to true on TogglyConfig.Builder:

TogglyConfig config = TogglyConfig.builder()
.appKey("YOUR_APP_KEY")
.environment("Production")
.refreshIntervalSeconds(30)
.enableLiveUpdates(true)
.build();

try (TogglyClient client = new TogglyClient(config)) {
client.refresh(); // First successful fetch starts the WebSocket connection.
// Run application work while this client stays open.
} // Close during application shutdown, not after each request.

Java uses java.net.http.WebSocket (JDK 11+) — no extra dependency.

How it works​

The HTTP provider starts a WebSocket only after a successful refresh path. Construction alone does not connect. A positive polling interval eventually attempts that first fetch; client.refresh() attempts it immediately. If the first download fails, onError reports the failure and configured defaults apply. A failed later refresh retains the last good definitions.

The SDK connects to the definitions worker (HttpSnapshotProvider):

wss://{baseURL}/{appKey}/ws?...

Update messages:

MessageSDK action
flags-updatedRefresh definitions
updateRefresh definitions (legacy)
signing-key-updatedClear JWKS, force refresh
pingIgnored

While the WebSocket is connected, scheduled HTTP refresh is skipped unless the fallback interval (FALLBACK_REFRESH_INTERVAL, ~20 minutes) elapses. Live updates accelerate freshness; the refresh loop still provides eventual consistency when the socket is disconnected if the polling interval is positive. An interval of zero disables polling, not streaming; with that setting, trigger the initial refresh yourself and plan for manual refresh if the socket cannot connect. A completed refresh attempt does not prove new data arrived.

See WebSocket sync (client-side) for the full protocol shared across SDKs.

Spring Boot​

Starter YAML properties do not currently expose enable-live-updates. The auto-configured TogglyConfig uses builder defaults (enableLiveUpdates = true). To disable streaming, override the TogglyConfig bean:

@Bean
public TogglyConfig togglyConfig() {
return TogglyConfig.builder()
.appKey(System.getenv("TOGGLY_APP_KEY"))
.environment("Production")
.enableLiveUpdates(false)
.build();
}

Use the explicit HTTP provider bean with this config; Spring owns provider and client shutdown.