> ## Documentation Index
> Fetch the complete documentation index at: https://docs.risingwave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sink data to NATS and NATS JetStream

> This guide describes how to sink data from RisingWave to NATS subjects using the NATS sink connector in RisingWave. Both NATS and NATS JetStream are supported by this connector.

[NATS](https://nats.io/) is an open source messaging system for cloud native applications. It provides a lightweight publish-subscribe architecture for high performance messaging.

[NATS JetStream](https://docs.nats.io/nats-concepts/jetstream) is a streaming data platform built on top of NATS. It enables real-time and historical access to streams of data via durable subscriptions and consumer groups.

## Prerequisites

Before sinking data from RisingWave to NATS or NATS JetStream, please ensure the following:

* The RisingWave cluster is running.
* A NATS or NATS JetStream server is running and accessible from your RisingWave cluster.
* Create a NATS subject that you want to sink data to.
* You have the permission to publish data to the NATS subject.

## Syntax

To sink data from RisingWave to a NATS subject, create a sink using the syntax below:

```sql theme={null}
CREATE SINK [ IF NOT EXISTS ] sink_name
[FROM sink_from | AS select_query]
WITH (
   connector='nats',
   server_url='<your nats server>:<port>', [ <another_server_url_if_available>, ...]
   subject='<your subject>',

 -- optional parameters
   allow_create_stream='<true|false>',
   connect_mode=<connect_mode>
   username='<your user name>',
   password='<your password>'
   jwt='<your jwt>',
   nkey='<your nkey>',
   type='<sink data type>'
);
```

After the sink is created, RisingWave will continuously sink data to the NATS subject in append-only mode.

<Note>
  The NATS sink connector in RisingWave provides at-least-once delivery semantics. Events may be redelivered in case of failures.
</Note>

<Note>
  **Stream creation security**

  Starting in v2.5, RisingWave does not create NATS streams automatically by default (`allow_create_stream = false`). This prevents accidental stream creation in production environments due to typos or misconfigurations.

  To enable automatic stream creation, set `allow_create_stream = 'true'`. Only enable this in development environments or when you specifically want RisingWave to manage stream creation.
</Note>

According to the [NATS documentation](https://docs.nats.io/running-a-nats-service/nats%5Fadmin/jetstream%5Fadmin/naming), stream names must adhere to subject naming rules as well as be friendly to the file system. Here are the recommended guidelines for stream names:

* Use alphanumeric values.
* Avoid spaces, tabs, periods (`.`), greater than (`>`) or asterisks (`*`).
* Do not include path separators (forward slash or backward slash).
* Keep the name length limited to 32 characters as the JetStream storage directories include the account, stream name, and consumer name.
* Avoid using reserved file names like `NUL` or `LPT1`.
* Be cautious of case sensitivity in file systems. To prevent collisions, ensure that stream or account names do not clash due to case differences. For example, `Foo` and `foo` would collide on Windows or macOS systems.

## Parameters

| Field                 | Notes                                                                                                                                                                                                                                                                                                                                                                                              |
| :-------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| server\_url           | **Required**. URLs of the NATS server, in the format of `address:port`. If multiple addresses are specified, use commas to separate them.                                                                                                                                                                                                                                                          |
| subject               | **Required**. NATS subject that you want to sink data to.                                                                                                                                                                                                                                                                                                                                          |
| allow\_create\_stream | **Optional**. Whether to allow RisingWave to create the NATS stream if it doesn't exist. Default: `false` (changed in v2.5). When `false`, RisingWave will return an error if the specified stream doesn't exist. When `true`, RisingWave will create the stream if it doesn't exist. For production environments, it's recommended to keep this as `false` to prevent accidental stream creation. |
| connect\_mode         | **Required**. Authentication mode for the connection. Allowed values: <ul><li>`plain`: No authentication;</li><li>`user_and_password`: Use user name and password for authentication. For this option, username and password must be specified;</li><li>`credential`: Use JSON Web Token (JWT) and NKeys for authentication. For this option, jwt and nkey must be specified.</li></ul>            |
| jwt and nkey          | JWT and NKEY for authentication. For details, see [JWT](https://docs.nats.io/running-a-nats-service/configuration/securing%5Fnats/auth%5Fintro/jwt) and [NKeys](https://docs.nats.io/running-a-nats-service/configuration/securing%5Fnats/auth%5Fintro/nkey%5Fauth).                                                                                                                               |
| username and password | **Conditional**. The client user name and password. Required when `connect_mode` is `user_and_password`.                                                                                                                                                                                                                                                                                           |
| type                  | **Required**. Sink data type. Its value should be `append-only`.                                                                                                                                                                                                                                                                                                                                   |
