Communicating with NATS using the HTTP protocol.
go install github.com/RussellLuo/hats@latest
Usage
$ hats -h
Usage of hats:
-config string
YAML/JSON file to read configuration from
-nats string
The URL of the NATS server (default "nats://127.0.0.1:4222")
-pub_addr string
The listen address of the publishing server (default ":8080")
-pub_key string
The auth key for the endpoints of the publishing server
-sub_stream -sub_stream
The JSON config of a single stream (Set multiple -sub_stream for multiple streams)
-sub_webhook_key string
The auth key of the default webhook
-sub_webhook_url string
The URL of the default webhook (default "http://127.0.0.1:8080/webhook")
Start a JetStream enabled NATS server (docs):
docker network create nats
docker run --name nats --network nats -p 4222:4222 -d nats -js
Create a stream (docs):
nats stream add ORDERS --subjects='ORDERS.*'
Run the hats
server:
hats -sub_stream='{"name":"ORDERS","consumers":[{"durable_name":"ORDERS_CONS"}]}'
For complex scenarios, you can also use a config file:
$ cat hats.yaml sub: streams: - name: ORDERS consumers: - durable_name: ORDERS_CONS $ hats -config=hats.yaml
Publish a message:
curl -XPOST 'http://127.0.0.1:8080/pub?subject=ORDERS.new' \
-H 'Content-Type: application/json' \
-d '{"order_id": "123"}'
The published message will be consumed by the default webhook handler, see logs of the hats
server.
Run the hats
server:
hats \
-sub_stream='{"name":"ORDERS","consumers":[{"durable_name":"ORDERS_CONS"}]}' \
-sub_webhook_url='http://127.0.0.1:8081/your-own-webhook' \
-sub_webhook_key='your-own-key'
Publish a message:
curl -XPOST 'http://127.0.0.1:8080/pub?subject=ORDERS.new' \
-H 'Content-Type: application/json' \
-d '{"order_id": "123"}'
The published message will be consumed by your own webhook handler, turn to your webhook server to see the results.
Checkout the Godoc.