nats
The nats module provides a NATS client for JSH applications.
Like the MQTT module, it is event-driven and automatically starts connecting when a Client is created.
Typical usage looks like this.
const nats = require('nats');Client
NATS client object.
Creation
new Client(options)Options
| Option | Type | Description |
|---|---|---|
servers | String[] | NATS server URLs such as nats://127.0.0.1:4222 |
name | String | Connection name |
user | String | Authentication user |
password | String | Authentication password |
token | String | Authentication token |
noRandomize | Boolean | Disable server randomization |
noEcho | Boolean | Disable echo of this client’s published messages |
verbose | Boolean | Enable verbose protocol behavior |
pedantic | Boolean | Enable pedantic protocol checks |
allowReconnect | Boolean | Enable reconnect handling |
maxReconnect | Number | Maximum reconnect attempts |
reconnectWait | Number | Reconnect wait in milliseconds |
timeout | Number | Connect timeout in milliseconds |
drainTimeout | Number | Drain timeout in milliseconds |
flusherTimeout | Number | Flush timeout in milliseconds |
pingInterval | Number | Ping interval in milliseconds |
maxPingsOut | Number | Maximum outstanding pings |
retryOnFailedConnect | Boolean | Retry when the first connection fails |
skipHostLookup | Boolean | Skip host lookup optimization |
Usage example
| |
Properties
config
client.config exposes the parsed native NATS configuration.
Representative fields include:
serversnameallowReconnectmaxReconnectreconnectWaittimeout
console.println(client.config.servers);
console.println(client.config.timeout);Methods
publish()
Publishes a message to a subject.
Syntax
publish(subject, message[, options])Parameters
subjectStringmessageString|Uint8Array|Object|ArrayoptionsObject(optional)
Supported options fields:
| Option | Type | Description |
|---|---|---|
reply | String | Reply subject for request/reply patterns |
Objects and arrays are JSON-encoded before publishing.
Return value
None. Result is delivered by published or error events.
subscribe()
Subscribes to a subject.
Syntax
subscribe(subject[, options])Parameters
subjectStringoptionsObject(optional)
Supported options fields:
| Option | Type | Description |
|---|---|---|
queue | String | Queue group name for queue subscriptions |
Return value
None. Result is delivered by subscribed or error events.
close()
Closes the client connection.
Syntax
close()Events
open
Emitted after the connection is established.
client.on('open', () => { ... })message
Emitted when a subscribed message is received.
client.on('message', (msg) => { ... })msg fields:
| Property | Type | Description |
|---|---|---|
topic | String | Alias of the subject for MQTT-style handlers |
subject | String | NATS subject |
reply | String | Reply subject for request/reply workflows |
payload | String | Message payload |
subscribed
Emitted when the server accepts a subscription.
client.on('subscribed', (subject, reason) => { ... })subjectStringreasonNumber
The current implementation uses 1 for successful subscribe acknowledgment.
published
Emitted after a publish request completes.
client.on('published', (subject, reason) => { ... })subjectStringreasonNumber
The current implementation uses 0 for successful publish completion.
error
Emitted when connection, subscribe, or publish fails.
client.on('error', (err) => { ... })errError
close
Emitted when close() is called.
client.on('close', () => { ... })Basic pub/sub example
| |
Request/reply example
For request/reply workflows, subscribe to a reply subject and publish with options.reply.
| |
Behavior notes
Clientstarts connecting automatically from the constructor.- Queue subscriptions are enabled through
subscribe(subject, { queue: 'workers' }). - Calling
publish()orsubscribe()before the connection is open emits anerrorevent. - Message payloads are exposed as strings by the current implementation.