### Install clj-nats library Source: https://github.com/cjohansen/clj-nats/blob/main/README.md Instructions for adding the clj-nats dependency to a Clojure project using Git coordinates, specifying the repository URL and a specific commit SHA. ```Clojure no.cjohansen/clj-nats {:git/url "https://github.com/cjohansen/clj-nats" :git/sha "LATEST_SHA"} ``` -------------------------------- ### Create a NATS Key/Value Store Bucket Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This example demonstrates how to initialize a NATS Key/Value store bucket using `kv/create-bucket`. The bucket is named 'my-kv-store' and configured to retain up to 24 revisions of each key's history. ```Clojure (require '[nats.core :as nats] '[nats.kv :as kv]) (def conn (nats/connect "nats://localhost:4222")) (kv/create-bucket conn {::kv/bucket-name "my-kv-store" ::kv/max-history-per-key 24}) ``` -------------------------------- ### Connect to NATS with Credentials and SSL Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet demonstrates how to establish an authenticated connection to a NATS server using credentials file path and an SSL context. It highlights key configuration options like server URL, connection name, and SSL setup. ```Clojure (require '[nats.core :as nats]) (import 'javax.net.ssl.SSLContext) (nats/connect {::nats/server-url "nats://your.nats.server:4222" ;; 1 ::nats/connection-name "myapp" ;; 2 ::nats/credentials-file-path "path/to/file.creds" ;; 3 ::nats/ssl-context (SSLContext/getDefault)}) ``` -------------------------------- ### Get NATS Server and Account Information Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet shows how to query the NATS server for a list of all streams and account-level statistics, providing an overview of the NATS environment. ```Clojure (require '[nats.stream :as stream]) (stream/get-streams conn) (stream/get-account-statistics conn) ``` -------------------------------- ### Retrieve NATS Stream Configuration and State Information Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet provides examples of functions to query various aspects of a NATS stream, including its configuration, current state, mirror information, and a list of its consumers. ```Clojure (require '[nats.stream :as stream]) (stream/get-stream-config conn "test-stream") (stream/get-stream-info conn "test-stream") (stream/get-stream-state conn "test-stream") (stream/get-mirror-info conn "test-stream") (stream/get-consumers conn "test-stream") ``` -------------------------------- ### Delete Key from NATS Key/Value Store Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This example shows how to remove a specific key and its associated value from a NATS Key/Value store using the `kv/delete` function. ```Clojure (kv/delete conn :my-kv-store/fruits) ``` -------------------------------- ### Publish Message to NATS Stream Subject Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This example shows how to publish a message to a specific subject within a NATS stream. It includes requiring necessary namespaces, establishing a connection, and using `stream/publish` to send a map as message data. ```Clojure (require '[nats.core :as nats] '[nats.stream :as stream]) (def conn (nats/connect "nats://localhost:4222")) (stream/publish conn {:nats.message/subject "test.work.email.ed281046-938e-4096-8901-8bd6be6869ed" :nats.message/data {:email/to "christian@cjohansen.no" :email/subject "Hello, world!"}}) ``` -------------------------------- ### Get History and Purge Key in NATS Key/Value Store Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet demonstrates how to retrieve the revision history for a specific key in a NATS Key/Value store using `kv/get-history` and how to completely purge all historical revisions for that key using `kv/purge`. ```Clojure (kv/get-history conn :my-kv-store/fruits) (kv/purge conn :my-kv-store/fruits) ``` -------------------------------- ### Implement NATS request/response in Clojure Source: https://github.com/cjohansen/clj-nats/blob/main/README.md Demonstrates the NATS request/response pattern. It sets up a subscriber to handle incoming requests and publish a response, and then shows how to send a request and await its response using `nats/request`. ```Clojure (require '[nats.core :as nats]) (def conn (nats/connect "nats://localhost:4222")) (def subscription (nats/subscribe conn "chat.>")) (.start (Thread. (fn [] (let [msg (nats/pull-message subscription 500)] (prn 'Request msg) (nats/publish conn {:nats.message/subject (:nats.message/reply-to msg) :nats.message/data {:message "Hi there, fella"}}))))) (def response (nats/request conn {:nats.message/subject "chat.general" :nats.message/data {:message "Hello world!"}})) (prn 'Response @response) (nats/unsubscribe subscription) (nats/close conn) ``` -------------------------------- ### Establish NATS connection in Clojure Source: https://github.com/cjohansen/clj-nats/blob/main/README.md Demonstrates how to establish a connection to a NATS server using the `nats/connect` function, specifying the server URL. This is the first step before performing any NATS operations. ```Clojure (require '[nats.core :as nats]) (def conn (nats/connect "nats://localhost:4222")) ``` -------------------------------- ### Connect to NATS with Clojure using various authentication methods Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet demonstrates how to establish a connection to a NATS server using the `nats/connect` function in Clojure. It illustrates three authentication approaches: providing credentials as static strings, using JWT and NKEY strings, and loading JWT and NKEY from files. The `::nats/auth-handler` key is used to specify the authentication mechanism. ```clj (require '[nats.core :as nats]) ;; Credentials (nats/connect {::nats/server-url "tls://your.nats.server:4222" ::nats/connection-name "myapp" ::nats/auth-handler (nats/create-static-auth-handler "...")}) ;; JWT + nkey (nats/connect {::nats/server-url "tls://your.nats.server:4222" ::nats/connection-name "myapp" ::nats/auth-handler (nats/create-static-auth-handler "...jwt" "...nkey")}) ;; JWT file and nkey file (nats/connect {::nats/server-url "tls://your.nats.server:4222" ::nats/connection-name "myapp" ::nats/auth-handler (nats/create-file-auth-handler "path/to/jwt.file" "path/to/nkey.file")}) ``` -------------------------------- ### Run NATS client tests using Make Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet shows how to execute the project's test suite using the `make test` command. A NATS server must be running on port 4222 for the tests to function correctly. The tests are designed to publish messages, create and delete streams and consumers, and clean up any resources they create. ```sh make test ``` -------------------------------- ### Create a NATS JetStream Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet demonstrates how to establish a connection to a NATS server and create a new JetStream. The stream is configured with a name, subjects it will listen to, direct access allowance, and a work-queue retention policy. ```Clojure (require '[nats.core :as nats] '[nats.stream :as stream]) (def conn (nats/connect "nats://localhost:4222")) (stream/create-stream conn {:nats.stream/name "test-stream" :nats.stream/subjects ["test.work.>"] :nats.stream/allow-direct? true :nats.stream/retention-policy :nats.retention-policy/work-queue}) ``` -------------------------------- ### Create and Review NATS Stream Consumer Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet illustrates how to create a durable consumer for a NATS stream and then retrieve its configuration information. The consumer is named 'test-consumer' and filters messages based on the 'test.work.>' subject. ```Clojure (require '[nats.core :as nats] '[nats.consumer :as consumer]) (def conn (nats/connect "nats://localhost:4222")) (consumer/create-consumer conn {:nats.consumer/stream-name "test-stream" :nats.consumer/name "test-consumer" :nats.consumer/durable? true :nats.consumer/filter-subject "test.work.>"}) ;; Review its configuration (consumer/get-consumer-info conn "test-stream" "test-consumer") ``` -------------------------------- ### Subscribe to NATS subject and pull messages in Clojure Source: https://github.com/cjohansen/clj-nats/blob/main/README.md Illustrates subscribing to a NATS subject using `nats/subscribe` and then pulling messages synchronously with `nats/pull-message`, specifying a timeout. It also demonstrates how to unsubscribe from a subscription. ```Clojure (require '[nats.core :as nats]) (def conn (nats/connect "nats://localhost:4222")) (def subscription (nats/subscribe conn "chat.>")) (def msg1 (nats/pull-message subscription 500)) ;; Wait up to 500ms (def msg2 (nats/pull-message subscription 500)) ;; Wait up to 500ms (nats/unsubscribe subscription) ``` -------------------------------- ### Put and Retrieve Key/Value Pairs in NATS KV Store Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This snippet illustrates how to store a key-value pair in a NATS KV store using `kv/put` and then retrieve it using `kv/get` (which returns a detailed entry map) or `kv/get-value` (which returns just the value). ```Clojure (kv/put conn :my-kv-store/fruits #{"Banana" "Apple"}) (kv/get conn :my-kv-store/fruits) (kv/get-value conn :my-kv-store/fruits) ``` -------------------------------- ### Publish message to NATS subject in Clojure Source: https://github.com/cjohansen/clj-nats/blob/main/README.md Shows how to publish a message to a NATS subject using `nats/publish`. The message is represented as a Clojure map, including the subject, data, and optional headers and reply-to subject. ```Clojure (require '[nats.core :as nats]) (def conn (nats/connect "nats://localhost:4222")) (nats/publish conn {:nats.message/subject "chat.general.christian" :nats.message/data {:message "Hello world!"} :nats.message/headers {:user "Christian"} ;; Optional :nats.message/reply-to "chat.general.replies"}) ;; Optional ``` -------------------------------- ### Consume and Acknowledge NATS Stream Messages Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This code demonstrates how to subscribe to a NATS stream consumer, pull a message with a timeout, and then acknowledge it. This is a common pattern for processing messages in a reliable manner. ```Clojure (require '[nats.core :as nats] '[nats.consumer :as consumer]) (def conn (nats/connect "nats://localhost:4222")) (with-open [subscription (consumer/subscribe conn "test-stream" "test-consumer")] (let [message (consumer/pull-message subscription 1000)] ;; Wait for up to 1000ms (consumer/ack conn message) (prn message))) ``` -------------------------------- ### Peek at Messages in NATS Stream Source: https://github.com/cjohansen/clj-nats/blob/main/README.md This code demonstrates how to retrieve specific messages from a NATS stream based on criteria like first/last message, sequence number, or next message after a given sequence and subject filter. ```Clojure (stream/get-first-message conn "test-stream" "test.work.email.*") (stream/get-last-message conn "test-stream" "test.work.email.*") (stream/get-message conn "test-stream" 3) (stream/get-next-message conn "test-stream" 2 "test.work.email.*") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.