chickadee » hiredis » redis-subscribe

redis-subscribe channel callbackprocedure

Subscribe to a Redis channel using pattern matching (internally uses the `PSUBSCRIBE` command).

Parameters:

  • channel: Channel pattern to subscribe to (string, supports wildcards like `*` and `?`)
  • callback: Callback function that receives each message

Callback Function: The callback receives a list with 4 elements for pattern messages: `("pmessage" "pattern" "channel" "message")`

The callback should return non-false to continue listening, or #f to unsubscribe.

;; Subscribe to all channels starting with "test"
(redis-subscribe "test*" 
  (lambda (reply)
    (let ((msg (list-ref reply 3)))
      (format #t "Received: ~A\n" msg)
      (not (string=? msg "quit")))))

;; Subscribe to a specific channel
(redis-subscribe "notifications"
  (lambda (reply)
    (let ((channel (list-ref reply 2))
          (message (list-ref reply 3)))
      (format #t "Channel ~A: ~A\n" channel message)
      #t))) ; Continue listening indefinitely