N
Novu8mo ago
DennisK

Does invoking a new Novu client hold a open connection?

Hi! Within my Remix app I would like to add Novu. Usually I wrap my clients in a remember/singleton function so it won't create another instance next to the other. Is this recommended as well or will Novu close the connection after it has been used?
3 Replies
Ask Inkeep
Ask Inkeep8mo ago
Hi @DennisK, I'm an AI assistant for Novu. While a team member reviews this question, I'll see if I can find relevant sources for your question. ----------------------------- While I generate an answer, here are some sources that might be helpful: * NextJS * Angular * Build a notification system for a blog site with React, NodeJS and Novu - Novu * Novu - The open-source notification infrastructure ----------------------------- Answer Novu, as an API client, does not persistently hold an open connection like a websocket might. Each API call is a separate HTTP request, and there is no continuous open connection maintained. It's efficient to use a singleton pattern for initializing the Novu client in your application. This way, you instantiate the Novu client once and reuse it throughout the lifecycle of your application, which is both memory and performance efficient. Here’s a basic example of how you might configure a singleton pattern for the Novu client in a JavaScript/Node.js environment:
import { Novu } from '@novu/node';

class NovuSingleton {
private static instance: Novu;

private constructor() {} // private constructor to prevent direct construction calls with the `new` operator.

public static getInstance(): Novu {
if (!NovuSingleton.instance) {
NovuSingleton.instance = new Novu(process.env.NOVU_API_KEY);
}
return NovuSingleton.instance;
}
}

export default NovuSingleton;
import { Novu } from '@novu/node';

class NovuSingleton {
private static instance: Novu;

private constructor() {} // private constructor to prevent direct construction calls with the `new` operator.

public static getInstance(): Novu {
if (!NovuSingleton.instance) {
NovuSingleton.instance = new Novu(process.env.NOVU_API_KEY);
}
return NovuSingleton.instance;
}
}

export default NovuSingleton;
This code ensures that Novu is instantiated only once and the same instance is reused, which aligns with your current approach of using a singleton or memory function for managing client instances. This approach does not hold open connections, and each use of NovuSingleton.getInstance() will use the same Novu client instance initialized with your API key. Glad I could be helpful. Feel free to create a new thread with any new questions you may have. Glad I could be helpful. Feel free to create a new thread with any new questions you may have.
Paweł T.
Paweł T.8mo ago
hey @DennisK! 👋 You don't have to use the singleton pattern to wrap the Novu client instance, because it's just the simple stateless "proxy" to our REST APIs. However if you would like to use the @novu/headless package to build the custom Notification Center UI, then it's recommended in that case, because of the WS connection that we establish in the background.
DennisK
DennisKOP8mo ago
Thanks! All is working.
Want results from more Discord servers?
Add your server