VOOZH about

URL: https://deepwiki.com/rudderlabs/rudder-php-sdk/3-consumer-transport-layer

⇱ Consumer Transport Layer | rudderlabs/rudder-php-sdk | DeepWiki


Loading...
Menu

Consumer Transport Layer

The Consumer Transport Layer is responsible for the final delivery of event data to the RudderStack Data Plane. It provides an abstraction that decouples the event-tracking API from the underlying network implementation. This allows the SDK to support various environments (CLI, Web Servers, background workers) and performance requirements (synchronous vs. asynchronous).

Consumer Abstraction

All transport implementations derive from the base Consumer class lib/Consumer/Consumer.php7-8 This base class defines the interface for the six core event types: track, identify, group, page, screen, and alias.

The SDK categorizes consumers into two primary patterns:

  1. Immediate Consumers: These process each event call individually as they occur (e.g., File).
  2. Queue Consumers: These inherit from QueueConsumer lib/Consumer/QueueConsumer.php7-8 which implements an in-memory buffer. Events are enqueued and dispatched in batches when specific thresholds (flush_at, max_batch_size_bytes) are met or when the process exits.

Transport Hierarchy and Code Mapping

The following diagram maps the logical transport types to their respective PHP classes and primary network functions.

Transport Implementation Mapping


Sources: lib/Consumer/Consumer.php7-114 lib/Consumer/QueueConsumer.php7-11 lib/Consumer/LibCurl.php7-9 lib/Consumer/ForkCurl.php7-9 lib/Consumer/Socket.php7-9 lib/Consumer/File.php7-9


Batching and Lifecycle

Most consumers utilize the QueueConsumer lifecycle to optimize performance by reducing the number of HTTP requests.

  1. Enqueue: When a tracking method is called, the message is added to an internal $queue array lib/Consumer/QueueConsumer.php149-171
  2. Threshold Check: The consumer checks if the queue has reached flush_at (default 100) or if size limits like max_queue_size_bytes (default 32MB) are exceeded lib/Consumer/QueueConsumer.php153-178
  3. Flush: The flush() method is triggered, which calls the implementation-specific flushBatch() lib/Consumer/QueueConsumer.php106-131
  4. Destruction: On script termination, the __destruct() method ensures any remaining events in the queue are flushed lib/Consumer/QueueConsumer.php97-101

For details on queue limits and batching logic, see Consumer Base Classes.


Built-in Transport Implementations

The SDK provides four concrete implementations to handle different deployment constraints:

ConsumerModeImplementationBest For
LibCurlSynchronousPHP ext-curlStandard web requests where reliability is prioritized over latency.
ForkCurlAsynchronousShell execHigh-performance web environments where "fire-and-forget" is required.
SocketAsynchronousRaw TCP SocketsEnvironments without curl or where non-blocking sockets are preferred.
FileLocal LoggingfwriteLogging to disk for later replay via CLI tools.

1. LibCurl Consumer

Uses the standard PHP cURL extension to send synchronous POST requests. It includes built-in exponential backoff for retrying failed requests (status codes 429, 5xx) lib/Consumer/LibCurl.php103-109 For details, see LibCurl Consumer.

2. ForkCurl Consumer

Spawns a background system process using exec to run a curl command. By appending > /dev/null 2>&1 &, it allows the PHP script to continue execution without waiting for a network response lib/Consumer/ForkCurl.php82-84 For details, see ForkCurl Consumer.

3. Socket Consumer

A low-level implementation using pfsockopen to create persistent streams lib/Consumer/Socket.php70-76 It manually constructs HTTP/1.1 request headers and handles response parsing lib/Consumer/Socket.php94-134 For details, see Socket Consumer.

4. File Consumer

The simplest consumer, which writes events as line-delimited JSON to a local log file lib/Consumer/File.php64-78 This is highly performant as it avoids network I/O during the request lifecycle. For details, see File Consumer.


Data Flow Diagram

The following diagram illustrates how an event moves from the Rudder facade through the QueueConsumer logic to a specific network implementation.

Event Dispatch Pipeline


Sources: lib/Consumer/QueueConsumer.php106-131 lib/Consumer/QueueConsumer.php149-178 lib/Consumer/LibCurl.php18-118