Home PHP Libraries Other libraries php-rdkafka client library
php-rdkafka client library

This is a client that can send and receive messages. Let me demonstrate the sending and receiving operations for you. Friends who need it can download it and try it out.

Send message

<?phptry {
    $rcf = new RdKafka\Conf();
    $rcf->set('group.id', 'test');
    $cf = new RdKafka\TopicConf();
    $cf->set('offset.store.method', 'broker');
    $cf->set('auto.offset.reset', 'smallest');
    $rk = new RdKafka\Producer($rcf);
    $rk->setLogLevel(LOG_DEBUG);
    $rk->addBrokers("127.0.0.1");
    $topic = $rk->newTopic("test", $cf);
    for($i = 0; $i < 1000; $i++) {
        $topic->produce(0,0,'test' . $i);//没有setMessge接口了,使用produce  参考:https://libraries.io/github/mentionapp/php-rdkafka
    } 
} catch (Exception $e) {
    echo $e->getMessage();

Receive message

<?phptry {
    $rcf = new RdKafka\Conf();
    $rcf->set('group.id', 'test');
    $cf = new RdKafka\TopicConf();/*
    $cf->set('offset.store.method', 'file');
*/
    $cf->set('auto.offset.reset', 'smallest');
    $cf->set('auto.commit.enable', true);
    $rk = new RdKafka\Consumer($rcf);
    $rk->setLogLevel(LOG_DEBUG);
    $rk->addBrokers("127.0.0.1");
    $topic = $rk->newTopic("test", $cf);    //$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);
    while (true) {
        $topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);
        $msg = $topic->consume(0, 1000);
        var_dump($msg);        if ($msg->err) {            echo $msg->errstr(), "\n";            break;
        } else {            echo $msg->payload, "\n";
        }
        $topic->consumeStop(0);
        sleep(1);
    }
} catch (Exception $e) {    echo $e->getMessage();
}


Disclaimer

All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn

Related Article

**Which Python SOAP Client Library Is Right for You? Navigating the Diverse Options and Their Documentation.** **Which Python SOAP Client Library Is Right for You? Navigating the Diverse Options and Their Documentation.**

25 Oct 2024

Diverse Python SOAP Client Libraries: Navigating the Documentation LabyrinthFor novice Python developers exploring SOAP and its client libraries,...

## What SOAP Client Library Should You Choose for Your Python Project? ## What SOAP Client Library Should You Choose for Your Python Project?

28 Oct 2024

SOAP Client Libraries for PythonIntroduction:When exploring SOAP technologies in Python, selecting an appropriate client library is crucial. This...

Memcache vs. Memcached: Which PHP Library Should You Choose? Memcache vs. Memcached: Which PHP Library Should You Choose?

09 Nov 2024

Distinguishing "Memcache" and "Memcached" in PHPPHP offers two memcached libraries: memcache and memcached. Understanding their differences helps...

Memcache vs Memcached: Which PHP Memcached Library Should You Choose? Memcache vs Memcached: Which PHP Memcached Library Should You Choose?

19 Nov 2024

Memcache vs Memcached: Choosing the Right PHP Memcached LibraryIntroductionPHP offers two seemingly similar memcached libraries: memcache and...

How Do I Link Static Libraries That Depend on Other Static Libraries? How Do I Link Static Libraries That Depend on Other Static Libraries?

13 Dec 2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

Which PHP Library Best Fits Your Email Address Validation Needs? Which PHP Library Best Fits Your Email Address Validation Needs?

18 Nov 2024

PHP Email Address Validation Libraries UncoveredEmail address validation plays a crucial role in data validation, but creating a...

See all articles