https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
a

aloof-computer-50221

02/22/2024, 8:46 PM
Hi , I am trying to use Growthbook in Nodejs but cannot make automatic streaming updates work. I have setup polyfills except the persistent cache. Also I did not setup middleware - I assume that this is not required for auto updates. or I am wrong? Do you have full NodeJs example somewhere?
h

happy-autumn-40938

02/22/2024, 8:48 PM
hi there. Are you able to provide a bit of sample code around gb SDK instantiation and where that sits in your backend?
a

aloof-computer-50221

02/22/2024, 8:50 PM
this is not the best piece of code 🙂 but here it is
Copy code
import { Injectable } from '@nestjs/common';
import { GrowthBook, Context } from '@growthbook/growthbook';
import { DoraServiceFeatures } from './features';



const GROWTHBOOK_CLIENTKEY = process.env.GROWTHBOOK_CLIENTKEY;
@Injectable()
export class GrowthBookService {
  private growthBook: GrowthBook;

  constructor() {

    const { setPolyfills } = require("@growthbook/growthbook");
    setPolyfills({
      // Required when using built-in feature loading and Node 17 or lower
      fetch: require("cross-fetch"),
      EventSource: require("eventsource"),
    });

    this.growthBook = new GrowthBook<DoraServiceFeatures>({
      apiHost: "<https://cdn.growthbook.io>",
      clientKey: GROWTHBOOK_CLIENTKEY,
      enableDevMode: true,
      subscribeToChanges: true
    });

    (async () => {
      await this.growthBook.loadFeatures({
        autoRefresh: true,
        timeout: 2000
      });
    })();
  }
this is NestJS app
h

happy-autumn-40938

02/22/2024, 9:02 PM
are you self-hosted or cloud? Also worth checking that your SDK Connections support streaming (they should by default if using cloud)
(I don't think we have a NodeJS example currently, but we can look into creating a reference project)
a

aloof-computer-50221

02/22/2024, 9:08 PM
cloud, streaming is enabled. how to troubleshoot.
h

happy-autumn-40938

02/22/2024, 10:27 PM
I'm not very familiar with NestJS. It would be helpful to know where this service is invoked. Is it once per incoming request? The JS SDK constructs a global "feature repository" singleton which is responsible for updating all request-specific SDK instances with the latest features. With streaming enabled (which your implementation should be supporting), these updates should be in real time. I'm wondering if something about NestJS is not allowing the feature repository singleton to survive.
It might be good to look at network calls from your backend to see if there is a long-lived GET issued to a
/sub
endpoint.
a

aloof-computer-50221

02/22/2024, 10:56 PM
Actually, managed to make it work. Combination of not being attentive 🙂, slightly misleading docs, plus some changes I was doing in UI.
h

happy-autumn-40938

02/22/2024, 11:22 PM
do you mind if i ask what was difficult about the docs so that I can patch them?
f

fresh-football-47124

02/23/2024, 5:43 AM
if you could share the code we can make some example docs for the next person
a

aloof-computer-50221

02/23/2024, 12:29 PM
Will try to write some details
For example this fragment from docs - // Optional, can make feature rollouts faster EventSource: require("eventsource") very misleading. you really need to do this if you want to have automatic updates.
Plus the example in the documentation is based on using middleware in Express. Of course, this is one option to implement FF, but you should present another example where you rely on automatic updates.
h

happy-autumn-40938

02/26/2024, 11:13 PM
thanks for the feedback! We'll have another look at our JS/Node docs
16 Views