Constructor
new NatsMessenger(config, logger)
Create a NatsMessenger instance
Parameters:
| Name | Type | Description |
|---|---|---|
config |
Config | The connect configuration of the NATS server |
logger |
Object | The central logger of the application |
Classes
Methods
(async) close()
Shut down the NATS adapter
Drain the connection to the NATS server and close it.
(async) drain()
Drain the connection to NATS
Draining provides for a graceful way to unsubscribe or close a connection
without losing messages that have already been dispatched to the client.
In general, you can drain a subscription or all subscriptions in a connection.
When you drain a subscription, the client sends an unsubscribe protocol message to the server followed by a flush.
The subscription handler is only removed after the server responds.
Thus, all pending messages for the subscription have been processed.
Draining a connection, drains all subscriptions.
However, when you drain the connection it becomes impossible to make new subscriptions or send new requests.
After the last subscription is drained, it also becomes impossible to publish a message.
These restrictions do not exist when just draining a subscription.
(async) flush()
Flushes the pending messages with NATS
Flush sends a PING to the server.
When the server responds with PONG you are guaranteed that all pending data was sent and received by the server.
publish(topic, payload, msgHeaders)
Publish payload and header to a topic
Parameters:
| Name | Type | Description |
|---|---|---|
topic |
String | The name of the topic (subject in NATS terminology) to that the message must be published. |
payload |
String | The payload of the message. |
msgHeaders |
Object | A flat object (key-value pairs) that represent the headers. |
request(topic, payload, timeout, headers) → {Object}
Send payload as a request message through the topic subject and expects a response until timeout.
Calls the given callback with the response.
Parameters:
| Name | Type | Description |
|---|---|---|
topic |
String | The subject to which the request will be sent. |
payload |
String | The content part of the message. |
timeout |
Number | Timeout in milliseconds, until the request waits for the response. |
headers |
Object | The key-value pairs of the request headers in the form of a plain old JavaScript object. |
Returns:
- It holds two properties:
-payload: The payload of the response
-headers: The key-value pairs of the response headers
- Type
- Object
response(topic, respCb) → {Object}
Setup response handler
Subscribes to the topic subject, and waits for incoming request messages.
When message arrives, calls the respCb with the incoming message
and headers and publish its return value and headers to the response subject defined by the incoming message.
Parameters:
| Name | Type | Description |
|---|---|---|
topic |
String | The name of the subject to wait for the request messages |
respCb |
function | The response callback with arguments of |
Returns:
- The subscription object
- Type
- Object
(async) start()
Start the NATS adapter
Create a connection to the NATS server using the configuration parameters
subscribe(topic, callback)
Subscribe to the topic subject, and calls the callback function with the inbound messages
so the messages will be processed asychronously.
Parameters:
| Name | Type | Description |
|---|---|---|
topic |
String | The subject that the subscriber will observe. |
callback |
function | A function, that the subscriber will call, with the following parameters: |