Hi , I am trying to use Growthbook in Nodejs but c...
# ask-questions
a
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
hi there. Are you able to provide a bit of sample code around gb SDK instantiation and where that sits in your backend?
a
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
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
cloud, streaming is enabled. how to troubleshoot.
h
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
Actually, managed to make it work. Combination of not being attentive 🙂, slightly misleading docs, plus some changes I was doing in UI.
h
do you mind if i ask what was difficult about the docs so that I can patch them?
f
if you could share the code we can make some example docs for the next person
a
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
thanks for the feedback! We'll have another look at our JS/Node docs