# rudder-sdk-node-develop

> Rudder Node SDK

Latest version **0.0.12** (published 2021-04-01) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install rudder-sdk-node-develop
pnpm add rudder-sdk-node-develop
yarn add rudder-sdk-node-develop
bun add rudder-sdk-node-develop
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2021-04-01 |
| First published | 2021-01-29 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 12 |
| Unpacked size | 36.1 KB |
| Known vulnerabilities | 0 (+27 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | rudderstack |
| Keywords | analytics |

## Links

- npm: https://www.npmjs.com/package/rudder-sdk-node-develop
- Repository: https://github.com/rudderlabs/rudder-sdk-node
- Homepage: https://github.com/rudderlabs/rudder-sdk-node#readme
- Issues: https://github.com/rudderlabs/rudder-sdk-node/issues
- npm.io page: https://npm.io/package/rudder-sdk-node-develop

## Dependencies (12)

- [ms](https://npm.io/package/ms.md) ^2.0.0
- [md5](https://npm.io/package/md5.md) ^2.2.1
- [bull](https://npm.io/package/bull.md) ^3.20.0
- [uuid](https://npm.io/package/uuid.md) ^3.2.1
- [axios](https://npm.io/package/axios.md) ^0.21.1
- [winston](https://npm.io/package/winston.md) ^3.3.3
- [axios-retry](https://npm.io/package/axios-retry.md) ^3.0.2
- [auto-changelog](https://npm.io/package/auto-changelog.md) ^1.16.2
- [lodash.isstring](https://npm.io/package/lodash.isstring.md) ^4.0.1
- [serialize-javascript](https://npm.io/package/serialize-javascript.md) ^5.0.1
- [remove-trailing-slash](https://npm.io/package/remove-trailing-slash.md) ^0.1.0
- [@segment/loosely-validate-event](https://npm.io/package/@segment/loosely-validate-event.md) ^2.0.0

## Recent versions

- 0.0.12 (latest) — 2021-04-01
- 0.0.11 — 2021-03-30
- 0.0.10 — 2021-03-23
- 0.0.9 — 2021-02-14
- 0.0.8 — 2021-02-03
- 0.0.7 — 2021-02-02
- 0.0.6 — 2021-02-02
- 0.0.5 — 2021-02-01
- 0.0.4 — 2021-02-01
- 0.0.3 — 2021-02-01
- 0.0.2 — 2021-02-01
- 0.0.1 — 2021-01-29

## README

# What is RudderStack?

[RudderStack](https://rudderstack.com/) is a **customer data pipeline** tool for collecting, routing and processing data from your websites, apps, cloud tools, and data warehouse.

More information on RudderStack can be found [here](https://github.com/rudderlabs/rudder-server).

# RudderStack Node.js SDK

RudderStack’s Node.js SDK allows you to track your customer event data from your Node.js code. Once enabled, the event requests hit the RudderStack servers. RudderStack then routes the events to the specified destination platforms as configured by you.

## Installation

```bash
$ npm install @rudderstack/rudder-sdk-node
```

## Usage

```js
const Analytics = require("@rudderstack/rudder-sdk-node");

// we need the batch endpoint of the Rudder server you are running
const client = new Analytics("write key", "<data-plane-uri>/v1/batch");

client.track({
  event: "event name",
  userId: "user id"
});
```

## Initialization for persistence

```
const client = new Analytics(
  "write_key",
  "server_url/v1/batch",
  {
    flushAt: <number> = 20,
    flushInterval: <ms> = 20000
    maxInternalQueueSize: <number> = 20000 // the max number of elements that the SDK can hold in memory,
                                                                // this is different than the Redis list created when persistence is enabled
  }
);
client.createPersistenceQueue({ redisOpts: { host: "localhost" } }, err => {})
```

Adding a method **createPersistenceQueue** which takes as input two params **queueOpts** and a **callback**

```
QueueOpts {
  queueName ?: string = rudderEventsQueue,
  isMultiProcessor ? : boolean = false
  prefix ? : string = {rudder},  // pass a value without the {}
  redisOpts : RedisOpts,
  jobOpts ?: JobOpts
}

https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queue
RedisOpts {
 port?: number = 6379;
 host?: string = localhost;
 db?: number = 0;
 password?: string;
}

JobOpts {
  maxAttempts ? : number = 10
}

callback: function(error) || function() // createPersistenceQueue calls this with error or nothing(in case of success), user
                                                                // should retry in case of error

```

The **createPersistenceQueue** method will initialize a Redis list by calling [Bull's](https://github.com/OptimalBits/bull) utility methods. It will also add a single job processor for processing(making requests to Rudder server) jobs that are pushed into the list. Before adding a processor, the SDK will remove the last active job(should be at max 1 active) if any and push it to be processed again in order. Error in doing this will lead to calling the **callback with error parameter**. Retry calling **createPersistenceQueue** with backoff.

If the **createPersistenceQueue** method is not called after initialising the SDK by the user, the SDK will work with no persistence and the behaviour will be same as at present.

### Flow

- Calling SDK apis like track, page, identify etc will push the events to an in-memory array
- the events from the array are flushed as a batch to Redis persistence based on the **flushAt** and **flushInterval** setting. Since, there is a communication to a separate process, tune the flushAt/flushInterval setting to slow down the hit to Redis. The in-memory array has a max size of **maxInternalQueueSize**, after which events won't be accepted if not drained at the other side (cases where Redis connection is slow etc)
- The processor will take the batch formed in the above step from Redis list and make a request to Rudder server. If succeeded, the job will be pushed to completed queue in Redis (Bull npm package terminology). In case of notRetryable error(4xx mainly), the job will be moved to failed queue (Bull npm package terminology). These queues are persistent and can be inspected directly in Redis or using an queue viewer [like](https://github.com/vcapretz/bull-board).
- In case of retryable errors, the job is pushed to completed queue and a new instance of job with the same batch data is created and pushed to front of the queue (to maintain order), so that the next job to be processed is the same (this is the retry functionality). This will go upto **JobOpts.maxAttempts** time with exponential backoff of power 2.
- If the job failed even after **JobOpts.maxAttempts**, it is moved to failed queue( explained above ).
- If the node process dies with jobs still in active state (not completed nor failed but in the process of sending/retrying), the **next time the SDK is initialised and createPersistenceQueue** is called, it will be removed and a new instance with the same batch data is pushed in front of the queue to picked up first by the processor.

### Important Parameters

- flushAt
- flushInterval
- maxInternalQueueSize
- JobOpts.maxAttempts

### Notes

- **isMultiProcessor** flag determines whether to handle previously active jobs, if false => the active job handling from previous processor is applied else Bull handles it by moving it to back of waiting queue.

### Limitation

https://gitter.im/OptimalBits/bull/archives/2018/04/17
**Details**: https://redis.io/topics/cluster-tutorial#redis-cluster-data-sharding
**Workaround**: https://gitter.im/OptimalBits/bull/archives/2018/04/17, we are passing a prefix with default {rudder}

## Documentation

Documentation is available [here](https://docs.rudderstack.com/rudderstack-sdk-integration-guides/rudderstack-node-sdk).

## Contact Us

If you come across any issues while configuring or using the RudderStack Node.js SDK, please feel free to [contact us](https://rudderstack.com/contact/) or start a conversation on our [Slack](https://resources.rudderstack.com/join-rudderstack-slack) channel. We will be happy to help you.

---
_Source: https://npm.io/package/rudder-sdk-node-develop · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
