Golang Walkthrough
This walkthrough covers publishing tasks and handling them in Go. A more thorough guide is planned. Complete Go reference documentation is available on pkg.go.dev.
The guide is known to work with Release 0.0.4.
Connecting to JetStream
A connection to a JetStream server is required. Either a prepared NATS connection or the name of a NATS Context can be passed in.
NATS supports many connection methods, security approaches, TLS or non-TLS, and websockets. See the nats.go package for details.
Passing in a prepared NATS connection:
Using a NATS Context called AJC:
In both cases, additional options can log disconnections, reconnections, and more.
Configuring queues
A queue holds messages for processing. Many named queues can coexist. Without an explicit queue, a default called DEFAULT is used.
Different queues support different concurrency limits, maximum attempts, and validity periods.
The call attaches to or creates a queue called EMAIL with specific options. If the queue exists, the client attaches without updating the configuration. Setting NoCreate: true prevents on-demand creation. See the Queue reference for details.
Creating and enqueueing tasks
A task can carry any payload that serializes to JSON. Task types such as email:new or email-new drive routing to handlers.
Any number of producers can create tasks from any number of processes.
A helper creates a map describing an email:
A new email task can then be created and enqueued:
The task is sent to the store and placed in the EMAIL work queue for processing.
Consuming and processing tasks
Messages are consumed and handled by matching their type on a specific queue. Task processors can run concurrently across processes, and each process can handle multiple tasks concurrently. Per-process and per-queue concurrency limits are configurable.
The following example uses more options than typical:
The example registers one handler for email:new with a callback that handles up to 10 tasks at a time.
Loading a task
Existing tasks can be loaded, including their status and other details: