Programmer Reference : Redis : EsRedisPubSubClient
EsRedisPubSubClient
Description
An instance of this class is a dedicated client for handling Redis's Publish/Subscribe (Pub/Sub) functionality. I wrap a standard <EsRedisClient> to manage the stateful, persistent connection required for receiving messages.
My primary role is to manage the lifecycle of subscriptions and to listen for incoming messages from the Redis server in a background process. When a message arrives, I dispatch it to the appropriate <EsRedisSubscription> object, which in turn delivers it to the user via an asynchronous EsStream.
Public API and Key Responsibilities
  • Creating a Pub/Sub client from an existing <EsRedisClient> using EsRedisPubSubClient on: aClient.
  • Subscribing to channels and patterns with subscribe: and psubscribe:, which return an <EsRedisSubscription> object.
  • Managing a non-blocking listener process that uses select() to efficiently wait for messages from Redis or internal shutdown signals.
  • Handling both RESP2 (Array-based) and RESP3 (Push-based) pub/sub messages transparently.
  • Ensuring thread-safe management of subscriptions.
Usage Example
| redisClient pubSubClient subscription |
redisClient := EsRedisClient host: '127.0.0.1' port: 6379.
redisClient connect.

pubSubClient := EsRedisPubSubClient on: redisClient.
subscription := pubSubClient subscribe: 'news-channel'.

subscription stream listen: [:message |
CwAppContext default asyncExecInUI: [Transcript show: message payload]].

" ... publish messages from another client ... "

subscription unsubscribe.
pubSubClient disconnect.
Collaborators
  • EsRedisClient: The underlying client used to send commands and access the network socket.
  • EsRedisSubscription: The object returned to the user that provides the EsStream of messages.
  • SciSocketManager: Used by the internal listener process for non-blocking I/O.
Instance Variables
  • redisClient - <EsRedisClient> The underlying standard client for communication.
  • subscriptions - <Dictionary> A map of channel/pattern names to their EsRedisSubscription objects.
  • accessLock - <Semaphore> A lock to ensure thread-safe access to the subscriptions dictionary and listener process state.
  • listenerProcess - <Process> The background process that actively listens for incoming messages.
  • controlPipe - <Dictionary> A pair of sockets used to signal the listener process to shut down gracefully.
Class Methods
on:
  Creates a new pub/sub client on an existing, connected EsRedis client.
    
     Arguments:
        aRedisClient - <EsRedisClient> An already connected instance of a Redis client.
     Answers:
        <EsRedisPubSubClient> A new, initialized instance of the receiver.
Instance Methods
disconnect
  Stops the listener process, releases all resources, and disconnects
     the underlying Redis client. This is the primary way to cleanly
     shut down the pub/sub client. This also returns the underlying client
     to its normal, non-subscriber state.
    
     Answers:
        <EsRedisPubSubClient>
isActive
  Answers whether the listener process is currently active.
    
     Answers:
        <Boolean>
psubscribe:
  Subscribes to a channel pattern and returns a subscription object whose stream will emit all received messages.
    
     Arguments:
        aPatternString - <String> The glob-style pattern to subscribe to.
     Answers:
        <EsRedisSubscription> A subscription object.
subscribe:
  Subscribes to a channel and returns a subscription object whose stream will emit all received messages.
    
     Arguments:
        aChannelName - <String> The name of the channel to subscribe to.
     Answers:
        <EsRedisSubscription> A subscription object.
Last modified date: 01/22/2026