On this page:
make-redis-pubsub
call-with-redis-pubsub
redis-pubsub?
redis-pubsub-kill!
redis-pubsub-publish!
redis-pubsub-subscribe!
redis-pubsub-unsubscribe!

12 PubSub Commands

procedure

(make-redis-pubsub client)  redis-pubsub?

  client : redis?
Puts client into PubSub mode and returns a value that can be used to subscribe to channels and receive messages.

PubSub values are synchronizable events whose synchronization results are the messages received via the channels the value is subscribed to.

Messages from channel subscriptions are represented as lists of two elements, where the first element is the name of the channel and the second the value of the message.

Messages from pattern subscriptions are represented as lists of three elements, where the first element is the pattern, the second the channel and the third the value of the message.

While in PubSub mode, any commands you try to send to the original connection will fail.

Here’s an example of how the pieces fit together:

(require redis)
 
(define pool
  (make-redis-pool))
 
(thread
 (lambda _
   (call-with-redis-client pool
     (lambda (c)
       (sleep 5)
       (redis-pubsub-publish! c "a" "hello")
       (sleep 5)
       (redis-pubsub-publish! c "a" "goodbye")))))
 
(call-with-redis-client pool
  (lambda (c)
    (call-with-redis-pubsub c
      (lambda (p)
        (redis-pubsub-subscribe! p "a")
        (displayln (~v (sync p)))
        (displayln (~v (sync p)))))))

procedure

(call-with-redis-pubsub client proc)  any

  client : redis?
  proc : (-> redis-pubsub? any)
Puts client into PubSub mode and calls proc with the resulting value, ensuring that the client is returned to a non-PubSub state once proc exits.

procedure

(redis-pubsub? v)  boolean?

  v : any/c
Returns #t when v is a pubsub value.

procedure

(redis-pubsub-kill! pubsub)  void?

  pubsub : redis-pubsub?
Unsubscribes pubsub from all channels and takes the underlying connection out of PubSub mode.

procedure

(redis-pubsub-publish! client    
  channel    
  message)  exact-nonnegative-integer?
  client : redis?
  channel : redis-string/c
  message : redis-string/c
Publishes message to channel and returns the total number of clients that received that message.
Commands used by this function: PUBLISH

procedure

(redis-pubsub-subscribe! pubsub    
  channel-or-pattern ...+    
  [#:patterns? patterns?])  void?
  pubsub : redis-pubsub?
  channel-or-pattern : redis-string/c
  patterns? : boolean? = #f
Subscribes pubsub to one or more channel-or-patterns.

When patterns? is #t, channel-or-pattern values are treated as patterns, otherwise they’re treated as channels.

procedure

(redis-pubsub-unsubscribe! pubsub    
  channel-or-pattern ...    
  [#:patterns? patterns?])  void?
  pubsub : redis-pubsub?
  channel-or-pattern : redis-string/c
  patterns? : boolean? = #f
Unsubscribes pubsub from one or more channel-or-patterns.

When patterns? is #t, channel-or-pattern values are treated as patterns, otherwise they’re treated as channels.