Node.js SDK
The 9n9s Node.js package provides a simple way to integrate Heartbeat Monitoring into your JavaScript or TypeScript applications.
Installation
Section titled “Installation”npm install @9n9s/sdkInitializing the Client
Section titled “Initializing the Client”First, initialize a client with your Heartbeat Monitor’s UUID. It’s best practice to store this in an environment variable.
import { Nines } from "@9n9s/sdk";
const nines = new Nines(process.env.MY_JOB_MONITOR_ID);Signaling Success
Section titled “Signaling Success”To send a simple success pulse:
await nines.pulse();Tracking Runtimes
Section titled “Tracking Runtimes”To track the execution time of a function, you can use the nines.time() wrapper. It will automatically send a /start pulse, execute your function, and then send a completion pulse. If your function throws an error, it will automatically send a /fail pulse with the error details.
async function myImportantJob() { // ... your job logic ... console.log("Doing work!");}
await nines.time(myImportantJob);You can also do this manually:
await nines.start();try { // ... your job logic ... await nines.pulse(); // or nines.success()} catch (error) { await nines.fail(error);}Sending Payloads
Section titled “Sending Payloads”You can send logs, error objects, or any other string payload with your pulses.
// Send a simple log messageawait nines.pulse("Processed 100 items.");
// Send an error object on failuretry { throw new Error("Database connection failed");} catch (error) { // The SDK will automatically serialize the error object await nines.fail(error);}Use with Serverless Functions
Section titled “Use with Serverless Functions”The SDK is great for monitoring serverless function executions.
// Example with an AWS Lambda handlerimport { Nines } from "@9n9s/sdk";
const nines = new Nines(process.env.MY_LAMBDA_MONITOR_ID);
export const handler = async (event) => { await nines.start(); try { // ... function logic ... const result = "OK"; await nines.pulse(`Execution finished with result: ${result}`); return { statusCode: 200, body: "Success" }; } catch (error) { await nines.fail(error); return { statusCode: 500, body: "Error" }; }};