Options
All
  • Public
  • Public/Protected
  • All
Menu

The LaunchDarkly SDK client object.

Applications should configure the client at startup time and continue to use it throughout the lifetime of the application, rather than creating instances on the fly.

Note that LDClient inherits from EventEmitter, so you can use the standard on(), once(), and off() methods to receive events. The client can emit the following kinds of events:

  • "ready": Sent only once, when the client has successfully connected to LaunchDarkly.
  • "failed": Sent only once, if the client has permanently failed to connect to LaunchDarkly.
  • "error": Contains an error object describing some abnormal condition that the client has detected (such as a network error).
  • "update": The client has received a change to a feature flag. The event parameter is an object containing the flag configuration; its key property is the flag key. Note that this does not necessarily mean the flag's value has changed for any particular user, only that some part of the flag configuration was changed.
  • "update:KEY": The client has received a change to the feature flag whose key is KEY. This is the same as "update" but allows you to listen for a specific flag.

For more information, see the SDK Reference Guide.

Hierarchy

  • EventEmitter
    • LDClient

Index

Properties

Static defaultMaxListeners

defaultMaxListeners: number

Methods

addListener

  • addListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

allFlags

  • allFlags(user: LDUser, callback?: undefined | function): Promise<LDFlagSet>
  • Retrieves the set of all flag values for a user.

    Deprecated: use allFlagsState instead. Current versions of the client-side SDK will not generate analytics events correctly if you pass the result of allFlags().

    Parameters

    • user: LDUser

      The end user requesting the feature flags.

    • Optional callback: undefined | function

      A Node-style callback to receive the result (as an LDFlagSet). If omitted, you will receive a Promise instead.

    Returns Promise<LDFlagSet>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result as an LDFlagSet.

allFlagsState

  • Builds an object that encapsulates the state of all feature flags for a given user. This includes the flag values and also metadata that can be used on the front end. This method does not send analytics events back to LaunchDarkly.

    The most common use case for this method is to bootstrap a set of client-side feature flags from a back-end service. Call the toJSON() method of the returned object to convert it to the data structure used by the client-side SDK.

    Parameters

    • user: LDUser

      The end user requesting the feature flags.

    • Optional options: LDFlagsStateOptions

      Optional LDFlagsStateOptions to determine how the state is computed.

    • Optional callback: undefined | function

      A Node-style callback to receive the result (as an LDFlagsState). If omitted, you will receive a Promise instead.

    Returns Promise<LDFlagsState>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result as an LDFlagsState.

close

  • close(): void
  • Discards all network connections, background tasks, and other resources held by the client.

    Do not attempt to use the client after calling this method.

    Returns void

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

flush

  • flush(callback?: undefined | function): Promise<void>
  • Flushes all pending analytics events.

    Normally, batches of events are delivered in the background at intervals determined by the flushInterval property of LDOptions. Calling flush() triggers an immediate delivery.

    Parameters

    • Optional callback: undefined | function

      A function which will be called when the flush completes. If omitted, you will receive a Promise instead.

    Returns Promise<void>

    If you provided a callback, then nothing. Otherwise, a Promise which resolves once flushing is finished. Note that the Promise will be rejected if the HTTP request fails, so be sure to attach a rejection handler to it.

getMaxListeners

  • getMaxListeners(): number
  • Returns number

identify

  • identify(user: LDUser): void
  • Identifies a user to LaunchDarkly.

    This simply creates an analytics event that will transmit the given user properties to LaunchDarkly, so that the user will be visible on your dashboard even if you have not evaluated any flags for that user. It has no other effect.

    If the user is omitted or has no key, the client will log a warning and will not send an event.

    Parameters

    • user: LDUser

      The user properties. Must contain at least the key property.

    Returns void

initialized

  • initialized(): boolean
  • Tests whether the client has completed initialization.

    If this returns false, it means that the client has not yet successfully connected to LaunchDarkly. It might still be in the process of starting up, or it might be attempting to reconnect after an unsuccessful attempt, or it might have received an unrecoverable error (such as an invalid SDK key) and given up.

    Returns boolean

    True if the client has successfully initialized.

isOffline

  • isOffline(): boolean
  • Tests whether the client is configured in offline mode.

    Returns boolean

    True if the offline property is true in your LDOptions.

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

off

  • off(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependListener

  • prependListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

secureModeHash

  • secureModeHash(user: LDUser): string
  • Computes an HMAC signature of a user signed with the client's SDK key.

    For more information, see the JavaScript SDK Reference Guide on Secure mode.

    Parameters

    • user: LDUser

      The user properties.

    Returns string

    The hash string.

setMaxListeners

  • setMaxListeners(n: number): this
  • Parameters

    • n: number

    Returns this

toggle

track

  • track(key: string, user: LDUser, data?: any): void
  • Tracks that a user performed an event.

    LaunchDarkly automatically tracks pageviews and clicks that are specified in the Goals section of the dashboard. This can be used to track custom goals or other events that do not currently have goals.

    If the user is omitted or has no key, the client will log a warning and will not send an event.

    Parameters

    • key: string

      The name of the event, which may correspond to a goal in A/B tests.

    • user: LDUser

      The user to track.

    • Optional data: any

      Optional additional information to associate with the event.

    Returns void

variation

  • Determines the variation of a feature flag for a user.

    Parameters

    • key: string

      The unique key of the feature flag.

    • user: LDUser

      The end user requesting the flag. The client will generate an analytics event to register this user with LaunchDarkly if the user does not already exist.

    • defaultValue: LDFlagValue

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional callback: undefined | function

      A Node-style callback to receive the result value. If omitted, you will receive a Promise instead.

    Returns Promise<LDFlagValue>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result value.

variationDetail

  • Determines the variation of a feature flag for a user, along with information about how it was calculated.

    The reason property of the result will also be included in analytics events, if you are capturing detailed event data for this flag.

    For more information, see the SDK reference guide.

    Parameters

    • key: string

      The unique key of the feature flag.

    • user: LDUser

      The end user requesting the flag. The client will generate an analytics event to register this user with LaunchDarkly if the user does not already exist.

    • defaultValue: LDFlagValue

      The default value of the flag, to be used if the value is not available from LaunchDarkly.

    • Optional callback: undefined | function

      A Node-style callback to receive the result (as an LDEvaluationDetail). If omitted, you will receive a Promise instead.

    Returns Promise<LDEvaluationDetail>

    If you provided a callback, then nothing. Otherwise, a Promise which will be resolved with the result (as an LDEvaluationDetail).

waitForInitialization

  • waitForInitialization(): Promise<LDClient>
  • Returns a Promise that tracks the client's initialization state.

    The Promise will be resolved if the client successfully initializes, or rejected if client initialization has irrevocably failed (for instance, if it detects that the SDK key is invalid).

    Note that you can also use event listeners (on) for the same purpose: the event "ready" indicates success, and "failed" indicates failure.

    Returns Promise<LDClient>

    A Promise that will be resolved if the client initializes successfully, or rejected if it fails. If successful, the result is the same client object.

waitUntilReady

  • waitUntilReady(): Promise<void>
  • Returns a Promise that tracks the client's initialization state.

    Deprecated: please use waitForInitialization instead. The difference between that method and this one is that waitUntilReady never rejects the Promise, even if initialization fails.

    Returns Promise<void>

    A Promise that will be resolved if the client initializes successfully.

Static listenerCount

  • listenerCount(emitter: EventEmitter, event: string | symbol): number
  • deprecated

    since v4.0.0

    Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number

Generated using TypeDoc