# @well-known-components/nats-component

> NATS component

Latest version **2.0.0** (published 2022-08-29) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @well-known-components/nats-component
pnpm add @well-known-components/nats-component
yarn add @well-known-components/nats-component
bun add @well-known-components/nats-component
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-08-29 |
| First published | 2022-06-17 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 31.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | well-known-components-publisher, menduz |

## Links

- npm: https://www.npmjs.com/package/@well-known-components/nats-component
- Repository: https://github.com/well-known-components/nats-component
- Homepage: https://github.com/well-known-components/nats-component#readme
- Issues: https://github.com/well-known-components/nats-component/issues
- npm.io page: https://npm.io/package/@well-known-components/nats-component

## Dependencies (4)

- [mitt](https://npm.io/package/mitt.md) ^3.0.0
- [nats](https://npm.io/package/nats.md) ^2.7.1
- [@well-known-components/interfaces](https://npm.io/package/@well-known-components/interfaces.md) ^1.1.1
- [@well-known-components/pushable-channel](https://npm.io/package/@well-known-components/pushable-channel.md) ^1.0.0

## Recent versions

- 2.0.0 (latest) — 2022-08-29
- 2.0.1-20220829190747.commit-5071389 (next) — 2022-08-29
- 1.0.0-20220808154534.commit-9d8321d (tag-1.0) — 2022-08-08
- 1.1.2-20220829185008.commit-c3a9100 — 2022-08-29
- 1.1.2-20220829184942.commit-2265470 — 2022-08-29
- 1.1.2-20220826200505.commit-faca1f3 — 2022-08-26
- 1.1.2-20220826200036.commit-637e542 — 2022-08-26
- 1.1.2-20220824191701.commit-dc6101a — 2022-08-24
- 1.1.2-20220819155512.commit-806e5ad — 2022-08-19
- 1.1.2-20220819155507.commit-5f7791c — 2022-08-19
- 1.1.1 — 2022-08-11
- 1.1.1-20220811172435.commit-4f5a901 — 2022-08-11
- 1.1.0 — 2022-08-09
- 1.0.0-20220805145749.commit-9d8321d — 2022-08-05
- 1.0.0-20220804163239.commit-93cb36f — 2022-08-04
- … 12 more at https://npm.io/package/@well-known-components/nats-component/versions

## README

# nats-component

A port used to communicate with [NATS](https://nats.io/), the cloud native messaging system.

## Configuration

-  `NATS_URL` environment variable to connect to the NATS node.

## API

### Create

```typescript
// src/components.ts
await createNatsComponent({ config, logs })
```

### Start

You'd normally won't have to call this function, as that is taken care by the [`Lifecycle.run`](https://github.com/well-known-components/interfaces) method. But the method will:

- Attempt to establish a connection with the NATS node
- Emit a `connected` event if the connection is successful

### Subscribe

Subscribe to a NATS topic

#### Example

```typescript
export async function setupTopics(globalContext: GlobalContext): Promise<void> {
  const { nats } = globalContext.components
  
  // Subscribe to the topic
  const connectSubscription = nats.subscribe('peer.*.connect')
  
  // Process messages received for the topic
  ;(async () => {
    for await (const message of connectSubscription.generator) {
      try {
        // Extract information from the subject for the topic
        // 'peer.837.connect' => '837'
        const id = message.subject.split('.')[1]
        
        // Parse the data from the message. Data is encoded as Uint8Array
        const data = message.data
      } catch (err: any) {
        logger.error(`cannot process peer_connect message ${err.message}`)
      }
    }
  })().catch((err: any) => logger.error(`error processing subscription message; ${err.message}`))
}
```

### Publish

Publish a message to a NATS topic

#### Example

```typescript
export async function setupTopics(globalContext: GlobalContext): Promise<void> {
  const { nats } = globalContext.components
  
  const peerId = '837'
  const payload = ... // Uint8Array
  
  // `payload` is optional
  components.nats.publish(`peer.${peerId.connect`, payload)
}
```

## Encode/Decode

Encode/Decode JSON messages using `JSONCodec`. Any other codec that encodes to `Uint8Array` can be used.

### Example using JSONCodec

```typescript
import { JSONCodec } from '@well-known-components/nats-component'

const jsonCodec = JSONCodec()
const jsonMessage = { id: 1 }

// Encode
const encodedMessage = jsonCodec.encode(jsonMessage)

// Decode
const decodedMessage = jsonCodec.decode(encodedMessage)
```

---
_Source: https://npm.io/package/@well-known-components/nats-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
