Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Dapr NATS Streaming pubsub component config

Configuration parameters

  • natsURL

    • description: NATS server URL
    • required: yes
    • sample value: nats://demo.nats.io:4222
  • natsStreamingClusterID

    • description: Cluster ID of the NATS streaming server
    • required: yes
    • sample value: test-cluster

Only the following options are applicable for both NATS streaming publishers and subscribers - natsURL, natsStreamingClusterID. The rest of the options are applicable only for subscribers applications only

  • subscriptionType

    • description: Type of NATS subscription
    • required: no (you can skip this if you don't want subscriber feature)
    • valid value(s): queueGroup (default), topic
  • durableSubscriptionName

    • description: name of durable subscription
    • required: no
    • sample value: myDurableSub

The options below capture subscription options for the NATS streaming client

Common rules for subscription options

  • These are not mandatory and you don't need to specify them if subscriber feature is not needed.
  • If you use them ensure that you specify only one of these. If multiple values are provided, only one will used and others will be ignored

List of options

  • deliverNew

    • description: specify this if you want to receive new messages only
    • valid value: "true" (does not accept any other value)
  • startAtSequence

    • description: message number to start delivering from (integer value)
    • sample value: "42"
  • startWithLastReceived

    • description: start delivering from last message
    • valid value: "true" (does not accept any other value)
  • deliverAll

    • description: start from the very first message delivering all of them
    • valid value: "true" (does not accept any other value)
  • startAtTimeDelta

  • startAtTime

  • startAtTimeFormat

    • description: Use only along with startAtTime option. This is the format for the time you have provided in the startAtTime option. see https://godoc.org/time#Parse for layouts
    • sample value: Mon Jan 2 15:04:05 -0700 MST 2006

Notes

Unique client IDs

NATS streaming requires unique client ID. These are auto-generated in the component rather than accepting a value from the user since that would not be viable in a dynamic env like kubernetes

Manual ACK

Subscription has been explicitly set fo manual ACK mode. In case of an error returned by the Dapr runtime, ACK will not be sent to NATS and will be redelivered as a result

Sample configurations for subscribers

To ensure that the subscriber gets only new messages after it starts

change subscriptionType to queue if you want to use a Queue group instead

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus-natsstreaming
spec:
  type: pubsub.natsstreaming
  metadata:
    - name: natsURL
      value: nats://localhost:4222
    - name: natsStreamingClusterID
      value: test-cluster
    - name: subscriptionType
      value: topic
    - name: deliverNew
      value: "true"

To ensure that the subscriber gets all the messages in the NATS channel

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus-natsstreaming
spec:
  type: pubsub.natsstreaming
  metadata:
    - name: natsURL
      value: nats://localhost:4222
    - name: natsStreamingClusterID
      value: test-cluster
    - name: subscriptionType
      value: topic
    - name: deliverAll
      value: "true"

To ensure that the subscriber wants to start at a specific message number i.e. sequence in the NATS channel

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus-natsstreaming
spec:
  type: pubsub.natsstreaming
  metadata:
    - name: natsURL
      value: nats://localhost:4222
    - name: natsStreamingClusterID
      value: test-cluster
    - name: subscriptionType
      value: topic
    - name: startAtSequence
      value: "42"

To ensure that the subscriber wants to start with the last delivered message (inclusive)

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus-natsstreaming
spec:
  type: pubsub.natsstreaming
  metadata:
    - name: natsURL
      value: nats://localhost:4222
    - name: natsStreamingClusterID
      value: test-cluster
    - name: subscriptionType
      value: topic
    - name: startWithLastReceived
      value: "true"

To use a durable subscription

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus-natsstreaming
spec:
  type: pubsub.natsstreaming
  metadata:
    - name: natsURL
      value: nats://localhost:4222
    - name: natsStreamingClusterID
      value: test-cluster
    - name: subscriptionType
      value: topic
    - name: durableSubscriptionName
      value: my-durable-sub
    - name: deliverNew
      value: "true"

Sample configuration for publisher

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus-natsstreaming
spec:
  type: pubsub.natsstreaming
  metadata:
    - name: natsURL
      value: nats://localhost:4222
    - name: natsStreamingClusterID
      value: test-cluster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment