VOOZH about

URL: https://deepwiki.com/hypervel/redis/5.4-pubsub-operations

⇱ Pub/Sub Operations | hypervel/redis | DeepWiki


Loading...
Menu

Pub/Sub Operations

Purpose and Scope

This document covers the Redis publish/subscribe functionality implemented in the RedisConnection class. It explains how subscribe and psubscribe commands handle timeout management and callback transformations to provide Laravel-compatible behavior.

For information about other command transformations, see Command Transformations. For the general transformation system, see Transformation System.

Overview

The RedisConnection class provides special handling for Redis pub/sub operations through two methods: subscribe and psubscribe. Unlike standard Redis commands, these operations:

  1. Block indefinitely waiting for messages
  2. Require custom timeout management to prevent connection pool issues
  3. Transform callback signatures to match Laravel's convention

Always-On Special Handling

Pub/sub operations receive special handling regardless of the shouldTransform flag. The __call method checks for subscribe and psubscribe commands at src/RedisConnection.php270-272 before checking the shouldTransform flag at src/RedisConnection.php274 This prioritization ensures:

  • Timeout management is always applied: Prevents connection pool corruption from blocking operations
  • Callback transformation is always performed: Ensures consistent Laravel-style signatures across all pub/sub usage

This differs from other command transformations (e.g., get, set, zadd) which only apply when shouldTransform=true.

Sources: src/RedisConnection.php267-287 </old_str>

<old_str>


</old_str> <new_str>



Sources: src/RedisConnection.php267-287 src/RedisConnection.php679-695

Subscribe Operation Flow

The following diagram illustrates the complete flow of a subscribe operation from invocation through callback execution:


Sources: src/RedisConnection.php267-287 src/RedisConnection.php679-695

Subscribe vs PSubscribe

The RedisConnection class supports two types of pub/sub operations with different callback signatures:

OperationPattern SupportCallback SignatureUse Case
subscribeNofn($message, $channel)Subscribe to specific channels by exact name
psubscribeYesfn($message, $channel)Subscribe to channels matching glob-style patterns

Both operations transform the native Redis callback signature to Laravel's convention, but psubscribe receives an additional pattern argument from Redis that is ignored in the transformation.

Subscribe Callback Transformation

The subscribe method receives channels and a callback, then transforms the callback signature:

Native Redis signature:


Transformed Laravel signature:


This transformation occurs in getSubscribeArguments at src/RedisConnection.php702-706

PSubscribe Callback Transformation

The psubscribe method handles pattern-based subscriptions with an additional pattern parameter:

Native Redis signature:


Transformed Laravel signature:


Note that the pattern parameter is discarded in the transformation. The transformation occurs at src/RedisConnection.php710-712

Sources: src/RedisConnection.php697-713

Timeout Management

Problem

Redis pub/sub operations block indefinitely while waiting for messages. If the connection pool's read timeout is enforced during subscription, the connection will be terminated prematurely, breaking the pub/sub functionality.

Solution

The callSubscribe method implements a three-phase timeout management strategy:


Implementation Details

PhaseActionPurpose
1. Save$timeout = $this->connection->getOption(Redis::OPT_READ_TIMEOUT)Store original timeout value
2. Disable$this->connection->setOption(Redis::OPT_READ_TIMEOUT, -1)Allow infinite blocking for pub/sub
3. Execute$this->connection->{$name}(...)Perform subscribe operation
4. Restore$this->connection->setOption(Redis::OPT_READ_TIMEOUT, $timeout)Reset timeout in finally block

The finally block ensures timeout restoration even if the subscription throws an exception, preventing corrupted connection state from being returned to the pool.

Sources: src/RedisConnection.php681-694

Code Structure Map

The following diagram maps the key methods involved in pub/sub operations:


Sources: src/RedisConnection.php267-287 src/RedisConnection.php679-713

Usage Patterns

Basic Subscribe Example


The connection automatically:

  • Detects the subscribe command via __call
  • Sets infinite timeout
  • Transforms the callback to Laravel style
  • Restores timeout when complete

Pattern Subscribe Example


The psubscribe operation works identically to subscribe but accepts glob patterns for channel names.

Multiple Channels


The callback receives the channel name as the second parameter, allowing channel-specific handling.

Sources: src/RedisConnection.php697-713

Implementation Details

Argument Wrapping

The getSubscribeArguments method uses Arr::wrap() to ensure the channels parameter is always an array:


This allows both single channel strings and channel arrays to be passed:

  • subscribe('channel1', $callback) → internally wrapped to ['channel1']
  • subscribe(['channel1', 'channel2'], $callback) → used as-is

Sources: src/RedisConnection.php699

Callback Signature Detection

The method distinguishes between subscribe and psubscribe based on the operation name:


The psubscribe callback receives four parameters from Redis but only forwards two to the application callback, discarding the $redis and $pattern parameters.

Sources: src/RedisConnection.php702-712

Error Handling

The timeout restoration in the finally block ensures that connection state remains consistent even if the subscription throws an exception:


Without this finally block, an exception during subscription would leave the connection with an infinite timeout, causing issues when the connection is reused from the pool for non-pub/sub operations.

Sources: src/RedisConnection.php686-694