Amplitude-TypeScript icon indicating copy to clipboard operation
Amplitude-TypeScript copied to clipboard

flush() call doesn't actually flush, but waits for the next automatic flush at configured interval instead?

Open ctizen opened this issue 1 year ago • 0 comments

Hello, I'm experiencing weird behavior of setGroup and flush methods of Amplitude NodeJS client in case when batching is enabled (useBatch: true).

Expected Behavior

I expect one of the following:

  • setGroup method sends request and resolves returned promise without any delays
  • Calling flush after setGroup sends all the requests and resolves all pending promises, including promise from setGroup

Current Behavior

The setGroup method waits until flush interval is over and only then it resolves the promise. The timeout is 10000ms by default (managed by flushIntervalMillis configuration option).

Manual calling of flush() doesn't help either. I've tried the following, no success:

  • const promise = ampli.setGroup(...); await Promise.all([ampli.flush(), promise]);
  • const promise = ampli.setGroup(...); ampli.flush(); await promise;

I digged into amplitude code and found this line - it's being reached when context.timeout is 0, and this is hardcoded when invoking method via execute() (see here).

What I'm trying to do here is to ensure the group is assigned in the amplitude before any other events both in service backend and frontend are sent. Thus, I need to wait until setGroup request is executed, and it might take up to 10 seconds which is not a satisfactory delay for a backend entrypoint. I've decreased flushIntervalMillis option to 1000ms for now and it helped (well, sort of), but it doesn't look like a proper solution.

Possible Solution

Please confirm if this is a bug or it's an expected behavior.

Environment

  • JS SDK Version: "@amplitude/analytics-node": "^1.0.1", actual installed version is 1.0.1. I'm pretty sure all behavior described above exists in latest version, according to what I can see in current master branch.
  • Installation Method: yarn
  • Browser and Version: Node 18.19.0

ctizen avatar Apr 12 '24 16:04 ctizen