No description
  • PHP 95.9%
  • Dockerfile 3.7%
  • Makefile 0.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2022-09-12 18:03:29 +03:00
.github/workflows Rename tests to phpunit 2022-09-12 18:03:14 +03:00
docs Add .gitattributes 2021-02-11 15:12:03 +03:00
examples Bump dependencies 2022-09-11 22:05:17 +03:00
src Fix and suppress static analyze errors 2022-09-11 23:07:46 +03:00
tests Tests with nsqd and utilities 2022-09-12 17:59:08 +03:00
.env Lookup (#14) 2021-09-04 01:55:34 +03:00
.gitattributes Add .gitattributes 2021-02-11 15:12:03 +03:00
.gitignore Tests with nsqd and utilities 2022-09-12 17:59:08 +03:00
.php-cs-fixer.php php-cs-fixer add types_spaces rule 2021-09-15 01:25:27 +03:00
composer.json Tests with nsqd and utilities 2022-09-12 17:59:08 +03:00
docker-compose.yml Leave one nsq service in favor of use docker-compose scale 2021-09-13 23:46:24 +03:00
Dockerfile Add Dockerfile 2021-01-28 01:11:32 +03:00
infection.json.dist Infection (#4) 2021-01-23 23:34:18 +03:00
LICENSE Initial commit 2021-01-19 20:33:35 +03:00
Makefile Add Makefile 2021-09-19 02:10:09 +03:00
phpstan.neon Fix and suppress static analyze errors 2022-09-11 23:07:46 +03:00
phpunit.xml.dist Add MessageTest 2021-01-23 03:39:35 +03:00
psalm.xml Bump dependencies 2022-09-11 22:05:17 +03:00
README.md Readme: fix main branch to master 2022-09-12 18:03:29 +03:00

Nsq PHP

PHP Client for NSQ.

Latest Stable Version Total Downloads License codecov Mutation testing badge telegram

This library follow SemVer. Until version 1.0 will be released anything MAY change at any time, public API SHOULD NOT be considered stable. If you want use it before stable version was released install strict version without range.

Installation

This library is installable via Composer:

composer require nsq/nsq

Requirements

This library requires PHP 8.0 or later.

Although not required, it is recommended that you install the phpinnacle/ext-buffer to speed up phpinnacle/buffer .

Features

  • PUB
  • SUB
  • Feature Negotiation
  • Discovery
  • Backoff
  • TLS
  • Deflate
  • Snappy
  • Sampling
  • AUTH

Usage

Producer

use Nsq\Producer;

$producer = Producer::create(address: 'tcp://nsqd:4150');

// Publish a message to a topic
$producer->publish('topic', 'Simple message');

// Publish multiple messages to a topic (atomically) 
$producer->publish('topic', [
    'Message one',
    'Message two',
]);

// Publish a deferred message to a topic
$producer->publish('topic', 'Deferred message', delay: 5000);

Consumer

use Nsq\Consumer;
use Nsq\Message;

$consumer = Consumer::create(
    address: 'tcp://nsqd:4150', 
    topic: 'topic',
    channel: 'channel',
    onMessage: static function (Message $message): Generator {
        yield $message->touch(); // Reset the timeout for an in-flight message        
        yield $message->requeue(timeout: 5000); // Re-queue a message (indicate failure to process)        
        yield $message->finish(); // Finish a message (indicate successful processing)        
    },
);

Lookup

use Nsq\Lookup;
use Nsq\Message;

$lookup = new Lookup('http://nsqlookupd0:4161');
$lookup = new Lookup(['http://nsqlookupd0:4161', 'http://nsqlookupd1:4161', 'http://nsqlookupd2:4161']);

$callable = static function (Message $message): Generator {
    yield $message->touch(); // Reset the timeout for an in-flight message        
    yield $message->requeue(timeout: 5000); // Re-queue a message (indicate failure to process)        
    yield $message->finish(); // Finish a message (indicate successful processing)        
};

$lookup->subscribe(topic: 'topic', channel: 'channel', onMessage: $callable);  
$lookup->subscribe(topic: 'anotherTopic', channel: 'channel', onMessage: $callable);

$lookup->unsubscribe(topic: 'local', channel: 'channel');
$lookup->stop(); // unsubscribe all  

Integrations

License:

The MIT License (MIT). Please see LICENSE for more information.