Skip to content

bogue1979/pod-bogue1979-nats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pod-bogue1979-nats

A pod to interact with NATS using babashka.

kv

(require '[babashka.pods :as pods])
(pods/load-pod "pod-bogue1979-nats")
(require '[pod.bogue1979.nats :as nats])

(def opts
  {:host "localhost"
   :nkey (slurp (str (System/getenv "HOME") "/.config/nats/local.seed")),
   :msg "_"
   :bucket "first-bucket"})

(prn (nats/kvput (merge opts {:key "foo" :value "bar"})))
(prn (nats/kvget (merge opts {:key "foo"})))

(nats/kvwatchbucket (fn [msg] (prn msg)) opts)
(deref (promise))

publish

(require '[babashka.pods :as pods])
(pods/load-pod "pod-bogue1979-nats")
;(some? (find-ns 'pod.bogue1979.nats))
(require '[pod.bogue1979.nats :as nats])

(def opts
  {:host "localhost"
   :nkey (slurp (str (System/getenv "HOME") "/.config/nats/local.seed")),
   :msg "{\"foo\": \"baz\"}"
   :subject "metrics.test"})

(prn (nats/publish opts))
;(prn (nats/publish (merge opts {:subject "not.allowed"})))

subscribe

(require '[babashka.pods :as pods]
         '[cheshire.core :as json :refer [parse-string]])
(pods/load-pod "pod-bogue1979-nats")
(require '[pod.bogue1979.nats :as nats])

(def opts
  {:host "localhost",
   :nkey (slurp (str (System/getenv "HOME") "/.config/nats/local.seed")),
   :subject "metrics.test"})

(defn prn-message
  [msg]
  (prn (try (parse-string (:data msg) true)
            (catch Exception _ {:error "Error parsing json", :message msg})
            (finally (:data msg)))))

(nats/subscribe (fn [x] (prn-message x)) opts)
(deref (promise))

request

nats reply metrics.test "answer"
(require '[babashka.pods :as pods])
(pods/load-pod "pod-bogue1979-nats")
(require '[pod.bogue1979.nats :as nats])

(def opts
  {:host "localhost"
   :nkey (slurp (str (System/getenv "HOME") "/.config/nats/local.seed")),
   :subject "metrics.test"
   :msg "request"
   :timeout_seconds 10})

(println (try
           (nats/request opts)
           (catch Exception e (str "Error: " (.getMessage e)))))