launchdarkly / node-server-sdk

LaunchDarkly Server-side SDK for Node
Other
79 stars 65 forks source link

Support listening to change events #133

Closed nadavkaner closed 5 years ago

nadavkaner commented 5 years ago

It will be nice to have the ability to listen to change events like there is in the js-client.

eli-darkly commented 5 years ago

There actually is such a mechanism. The client will emit an event called "update:MY_FLAG_KEY" for each specific flag that's changed, as well as a general "update" event for any flag change; to listen for these events you would just do client.on(nameOfEvent, callback).

There are a few issues:

  1. We failed to document it, so there's no way you could have known.
  2. We left the on method out of the TypeScript definitions in index.d.ts, so if you're using TypeScript it won't be available to you until we fix that.
  3. There's a basic difference between server-side and client-side SDKs which means that unlike the js-client change event, it can't tell you what the new value of the flag is— because that varies per user, and the server-side SDKs don't have any notion of a "current user." So the event will just be telling you that the definition of the flag has changed, and then you would need to call variation with a specific user in order to see what the value is now for that user (which, depending on what was changed in the flag, might end up being the same value as before). This makes the update events somewhat less useful in a server-side context, which is one reason we haven't implemented them more widely yet.
  4. There is a potential problem in the current implementation, if you're using prerequisite flags and/or user segments. It will tell you if, for instance, flag A has been edited. But if A has B as a prerequisite, editing B will only trigger an update for B— not for A, even though its behavior is dependent on B. The same is true if you edit a user segment that is referenced in a flag rule.
eli-darkly commented 5 years ago

Correction to the previous comment: the on and off methods were always available in TypeScript, because LDClient inherits from EventEmitter.

Also, these events are now documented in the TypeScript declarations for the SDK (index.d.ts) which currently are the best reference for all existing methods and properties. Once we have online generated documentation, this information will be there too.