taoensso / carmine

Redis client + message queue for Clojure
https://www.taoensso.com/carmine
Eclipse Public License 1.0
1.15k stars 130 forks source link

How to use pub/sub with Integrant? #249

Closed hivesy closed 3 years ago

hivesy commented 3 years ago

In the docs, the code for the listener is available like this:

(def listener
  (car/with-new-pubsub-listener (:spec server1-conn)
    {"foobar" (fn f1 [msg] (println "Channel match: " msg))
     "foo*"   (fn f2 [msg] (println "Pattern match: " msg))}
   (car/subscribe  "foobar" "foobaz")
   (car/psubscribe "foo*")))

Now how to replace the server1-conn, if the conn is declared using integrant?

(defmethod ig/init-key :app/redis [_ {:keys [redis-host
                                                 redis-port] :as opts}]
  (timbre/warn "Connecting to the Redis server" opts)
  (let [conn {:pool {}
              :spec {:host redis-host
                     :port redis-port}}]

    (try
      (timbre/warn "Connected to the Redis - " (redis/wcar conn (redis/ping)))
      ;; Return the redis connection object
      conn
      ;; When redis is not available then retrun nil
      (catch Exception _
        (timbre/warn "Unbale to connect with the Redis server ")
        nil))))