JavaScript SDK
Flipswitch SDK for JavaScript/TypeScript with real-time SSE support
JavaScript SDK
The Flipswitch JavaScript SDK provides an OpenFeature-compatible provider with automatic cache invalidation via Server-Sent Events (SSE).
Installation
For server-side (Node.js), use @openfeature/server-sdk instead:
Quick Start
Browser (React, Vue, etc.)
Node.js (Server)
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | required | Environment API key from dashboard |
baseUrl | string | https://api.flipswitch.dev | Your Flipswitch server URL |
enableRealtime | boolean | true | Enable SSE for real-time flag updates |
pollingInterval | number | 30000 | Cache TTL in milliseconds |
fetchImplementation | typeof fetch | fetch | Custom fetch function |
Real-Time Updates
When enableRealtime is enabled (default), the SDK maintains an SSE connection to receive instant flag changes:
- The SSE client receives a
flag-changeevent - The local cache is immediately invalidated
- Next flag evaluation fetches the fresh value
- OpenFeature emits a
PROVIDER_CONFIGURATION_CHANGEDevent
Event Handlers
Connection Status
React Integration
The useFlag hook automatically re-renders when flags change via SSE.
Evaluation Context
Pass user attributes for targeting:
Bulk Flag Evaluation
Evaluate all flags at once or get detailed evaluation results:
Reconnection Strategy
The SSE client automatically reconnects with exponential backoff:
- Initial delay: 1 second
- Maximum delay: 30 seconds
- Backoff multiplier: 2x
When reconnected, the provider status changes from STALE back to READY.
TypeScript Support
The SDK is written in TypeScript and provides full type definitions:
Troubleshooting
SSE connection keeps disconnecting
Check if your server or proxy supports long-lived connections. Some proxies timeout after 30-60 seconds.
Flags not updating in real-time
Verify that enableRealtime is true (default) and check the SSE status:
CORS errors
Ensure your Flipswitch server has CORS configured to allow requests from your domain.