Skip to content

Implement Consumer Push API#40

Merged
kafkiansky merged 12 commits into0.3.xfrom
impl/push-consumer-api
Nov 12, 2025
Merged

Implement Consumer Push API#40
kafkiansky merged 12 commits into0.3.xfrom
impl/push-consumer-api

Conversation

@kafkiansky
Copy link
Collaborator

@kafkiansky kafkiansky commented Nov 10, 2025

Closes #28

This PR makes the Push Consumer API available for use, which is also utilized by KV and Object Store to implement key watching functionality.

With proper configuration push consumers enable more reliable processing of individual messages where batch processing is not required. They provide predictable message balancing and allow for scaling by increasing the number of consumers, similar to how consumers and prefetch count work in RabbitMQ.

The Push Consumer API mirrors the Pull Consumer API to ensure a seamless transition between the two models. All you need to do is replace $consumer->pull with $consumer->push:

<?php

declare(strict_types=1);

require_once __DIR__ . '/vendor/autoload.php';

use Thesis\Nats;
use Thesis\Nats\JetStream\Api;
use Thesis\Time\TimeSpan;

$nc = new Nats\Client(Nats\Config::default());
$js = $nc->jetStream();

$stream = $js->createOrUpdateStream(new Api\StreamConfig(
    name: 'EventsStream',
    description: 'Testing Stream',
    subjects: ['events.*'],
));

$consumer = $stream->createOrUpdateConsumer(new Api\ConsumerConfig(
    durableName: 'EventPushConsumer',
    deliverSubject: 'push-consumer-delivery',
    ackPolicy: Api\AckPolicy::Explicit,
    idleHeartbeat: TimeSpan::fromSeconds(5),
    maxAckPending: 1,
));

$subscription = $consumer->push(
    static function (Nats\JetStream\Delivery $delivery): void {
        dump($delivery->message->payload);
        $delivery->ack();
    },
);

$subscription->awaitCompletion();

$nc->drain();

@kafkiansky kafkiansky self-assigned this Nov 10, 2025
@kafkiansky kafkiansky merged commit a3d5ad8 into 0.3.x Nov 12, 2025
18 checks passed
@kafkiansky kafkiansky deleted the impl/push-consumer-api branch November 15, 2025 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose push consumer api

1 participant