taoensso / carmine

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

car/with-new-pubsub-listener & and car/set in functions #286

Closed matteoredaelli closed 1 year ago

matteoredaelli commented 1 year ago

Hello

I can replicate correctly the sample about pub / sub with "println" but I cannot add a car/set in the listener function

  (def listener
    (car/with-new-pubsub-listener my-conn-spec
      {
       "foo"  (fn f1
                [msg]
                (println msg)
                (car/set "a" "1")
                )
       "foo*"   (fn f2 [msg] (println "Pattern match: " msg))}
      (car/subscribe  queue-name)
      (car/psubscribe "foo*")
      ))

When I publish a sample message

127.0.0.1:6379> PUBLISH foo c
(integer) 2
127.0.0.1:6379> get a
"2"

I get

[subscribe foo 1]
Pattern match:  [psubscribe foo* 2]

[message foo c]
[pmessage foo* foo c]

I do not get any errors, but the value of the key "a" is not updated.

What's wrong with my code?

Thanks in advance Matteo

ptaoussanis commented 1 year ago

@matteoredaelli Hi Matteo, car/set and other Redis commands need to be called within a wcar context.

The listener's connection context is intentionally separate. For example you may want the listener to receive messages from a particular connection, but send commands via a different connection.

Hope that helps!