Git Product home page Git Product logo

phpnsq's Introduction

phpnsq

Build Status

Install

composer require okstuff/phpnsq

Subscribe one topic and channel

php examples/console phpnsq:sub <topic> <channel>

Notice

Before try this, you should install nsq by yourself.

Examples

  1. Publish
<?php
require __DIR__ . "/../vendor/autoload.php";

use OkStuff\PhpNsq\PhpNsq;

$config = require __DIR__ . '/../src/config/phpnsq.php';
$phpnsq = new PhpNsq($config);

//normal publish
$msg = $phpnsq->setTopic("sample_topic")->publish("Hello nsq.");
var_dump($msg);

//defered publish
$msg = $phpnsq->setTopic("sample_topic")->publishDefer("this is a defered message.", 10);
var_dump($msg);

//multiple publish
$messages = [
    "Hello, I am nsq client",
    "There are so many libraries developed by PHP",
    "Oh, no, PHP is not so good and slowly",
];
$msg = $phpnsq->setTopic("sample_topic")->publishMulti(...$messages);
var_dump($msg);
  1. Subscribe
<?php

namespace OkStuff\PhpNsq\Cmd;

use OkStuff\PhpNsq\Stream\Message;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Subscribe extends Base
{
    CONST COMMAND_NAME = 'phpnsq:sub';

    public function configure()
    {
        $this->setName(self::COMMAND_NAME)
            ->addArgument("topic", InputArgument::REQUIRED, "The topic you want to subscribe")
            ->addArgument("channel", InputArgument::REQUIRED, "The channel you want to subscribe")
            ->setDescription('subscribe new notification.')
            ->setHelp("This command allows you to subscribe notifications...");
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $phpnsq = self::$phpnsq;
        $phpnsq->setTopic($input->getArgument("topic"))
            ->setChannel($input->getArgument("channel"))
            ->subscribe($this, function (Message $message) use ($phpnsq, $output) {
                // $output->writeln($message->toJson());
                $phpnsq->getLogger()->info("READ", $message->toArray());
            });
        //excuted every five seconds.
        $this->addPeriodicTimer(5, function () use ($output) {
            $memory    = memory_get_usage() / 1024;
            $formatted = number_format($memory, 3) . 'K';
            $output->writeln(date("Y-m-d H:i:s") . " ############ Current memory usage: {$formatted} ############");
        });
        $this->runLoop();
    }
}
  1. Console
#!/usr/bin/env php
<?php

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

use Symfony\Component\Console\Application;
use OkStuff\PhpNsq\Cmd\Subscribe;

$application = new Application();

$config = require __DIR__.'/../src/config/phpnsq.php';

$application->add(new Subscribe($config, null));

$application->run();

phpnsq's People

Contributors

frankfarmer avatar yerstd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phpnsq's Issues

Subscribe examples

Hello, project looks interesting, can you update simple subscribe examples and document it a bit.
Thx

Given examples does not work

Hey, this package looked promising so I gave it a try. Unfortunately, running provided examples does not give expected results. In general I tried to publish three messages like this:

$phpnsq->publish('Message #1');
$phpnsq->publishDefer('Message #2', 10);
$phpnsq->publishMulti('Message #3', 'Message #4');

Dunno why, but only the first message is being published. Sometimes deferred publish works too but that's all. It works only when I add a small delay between:

$phpnsq->publish('Message #1');
sleep(1);
$phpnsq->publishDefer('Message #2', 10);
$phpnsq->publishMulti('Message #3', 'Message #4');

Either way this is what I get from the nsqd using dev branch ("connection reset by peer"):

nsqd_1        | [nsqd] 2020/04/21 13:46:29.888647 INFO: TCP: new client(172.21.0.6:47026)
nsqd_1        | [nsqd] 2020/04/21 13:46:29.888815 INFO: CLIENT(172.21.0.6:47026): desired protocol magic '  V2'
nsqd_1        | [nsqd] 2020/04/21 13:46:29.889138 INFO: [172.21.0.6:47026] IDENTIFY: {ClientID: Hostname: HeartbeatInterval:0 OutputBufferSize:0 OutputBufferTimeout:0 FeatureNegotiation:false TLSv1:true Deflate:false DeflateLevel:0 Snappy:false SampleRate:0 UserAgent: MsgTimeout:0}
nsqd_1        | [nsqd] 2020/04/21 13:46:29.889356 INFO: PROTOCOL(V2): [172.21.0.6:47026] exiting ioloop
nsqd_1        | [nsqd] 2020/04/21 13:46:29.889395 ERROR: client(172.21.0.6:47026) - failed to send response - write tcp 172.21.0.3:4150->172.21.0.6:47026: write: connection reset by peer
nsqd_1        | [nsqd] 2020/04/21 13:46:29.889407 INFO: PROTOCOL(V2): [172.21.0.6:47026] exiting messagePump

or using latest release ("broken pipe"):

nsqd_1        | [nsqd] 2020/04/21 13:57:30.617509 INFO: TCP: new client(172.21.0.6:47306)
nsqd_1        | [nsqd] 2020/04/21 13:57:30.617534 INFO: CLIENT(172.21.0.6:47306): desired protocol magic '  V2'
nsqd_1        | [nsqd] 2020/04/21 13:57:30.618808 INFO: PROTOCOL(V2): [172.21.0.6:47306] exiting ioloop
nsqd_1        | [nsqd] 2020/04/21 13:57:30.618856 ERROR: client(172.21.0.6:47306) - failed to send response - write tcp 172.21.0.3:4150->172.21.0.6:47306: write: broken pipe
nsqd_1        | [nsqd] 2020/04/21 13:57:30.618872 INFO: PROTOCOL(V2): [172.21.0.6:47306] exiting messagePump

I'm using default NSQ configuration. The problem does not exist when I'm using php-nsq in the same environment, but phpnsq looks much better. Any idea?

Your requirements could not be resolved to an installable set of packages.

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for okstuff/phpnsq dev-master -> satisfiable by okstuff/phpnsq[dev-master].
- Conclusion: remove monolog/monolog 2.x-dev
- Conclusion: don't install monolog/monolog 2.x-dev
- okstuff/phpnsq dev-master requires monolog/monolog ^1.23|^1.24 -> satisfiable by monolog/monolog[1.23.0, 1.24.0, 1.25.0, 1.25.1, 1.25.2, 1.25.3, 1.x-dev].
- Can only install one of: monolog/monolog[1.23.0, 2.x-dev].
- Can only install one of: monolog/monolog[1.24.0, 2.x-dev].
- Can only install one of: monolog/monolog[1.25.0, 2.x-dev].
- Can only install one of: monolog/monolog[1.25.1, 2.x-dev].
- Can only install one of: monolog/monolog[1.25.2, 2.x-dev].
- Can only install one of: monolog/monolog[1.25.3, 2.x-dev].
- Can only install one of: monolog/monolog[1.x-dev, 2.x-dev].
- Installation request for monolog/monolog (locked at 2.x-dev) -> satisfiable by monolog/monolog[2.x-dev].

Can only install one of: monolog/monolog[2.0.0, 1.25.4]

  • okstuff/phpnsq 3.1 requires monolog/monolog ^2.0 -> satisfiable by monolog/monolog[2.0.0, 2.0.1, 2.0.2, 2.1.0].
  • Can only install one of: monolog/monolog[2.0.0, 1.25.4].
  • Can only install one of: monolog/monolog[2.0.1, 1.25.4].
  • Can only install one of: monolog/monolog[2.0.2, 1.25.4].
  • Can only install one of: monolog/monolog[2.1.0, 1.25.4].
  • Installation request for monolog/monolog (locked at 1.25.4) -> satisfiable by monolog/monolog[1.25.4].

Warning: Undefined array key "producers" in /vendor\okstuff\phpnsq\src\phpnsq\Conn\Lookupd.php on line 48

ERROR:

Warning: Undefined array key "producers" in C:\xampp6\htdocs\api\vendor\okstuff\phpnsq\src\phpnsq\Conn\Lookupd.php on line 48

Warning: foreach() argument must be of type array|object, null given in C:\xampp6\htdocs\api\vendor\okstuff\phpnsq\src\phpnsq\Conn\Lookupd.php on line 48

Fatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty in C:\xampp6\htdocs\api\vendor\okstuff\phpnsq\src\phpnsq\Conn\Pool.php:50 Stack trace: #0 C:\xampp6\htdocs\api\vendor\okstuff\phpnsq\src\phpnsq\Conn\Pool.php(50): array_rand(Array) #1 C:\xampp6\htdocs\api\vendor\okstuff\phpnsq\src\phpnsq\PhpNsq.php(57): OkStuff\PhpNsq\Conn\Pool->getConn() #2 C:\xampp6\htdocs\api\abc.php(12): OkStuff\PhpNsq\PhpNsq->publish('Hello nsq.') #3 {main} thrown in C:\xampp6\htdocs\api\vendor\okstuff\phpnsq\src\phpnsq\Conn\Pool.php on line 50

My Code is

require DIR . "/vendor/autoload.php";

use OkStuff\PhpNsq\PhpNsq;

$config = require DIR . '/vendor/okstuff/phpnsq/src/config/phpnsq.php';
$phpnsq = new PhpNsq($config);

//normal publish
$msg = $phpnsq->setTopic("sample_topic")->publish("Hello nsq.");
var_dump($msg);
exit();

//defered publish
$msg = $phpnsq->setTopic("sample_topic")->publishDefer("this is a defered message.", 10);
var_dump($msg);

//multiple publish
$messages = [
"Hello, I am nsq client",
"There are so many libraries developed by PHP",
"Oh, no, PHP is not so good and slowly",
];
$msg = $phpnsq->setTopic("sample_topic")->publishMulti(...$messages);
var_dump($msg);

Please help to resolve the issue. Thanks

Issue with monolog

Using symfony 4 there are issues when calling constructor new Nsq() like:

use OkStuff\PhpNsq\Message\Message;
use OkStuff\PhpNsq\PhpNsq;
use OkStuff\PhpNsq\Command\Base;

        $this->config = [
            "nsq" => [
                "nsqd-addrs" => [
                    "127.0.0.1:4150",
                ],
                "logdir" => "/tmp",
            ],
        ];

        $this->client = new PhpNsq($this->config);

        var_dump($this->client); exit;

It' triggered error even when force to use Logging from vendor PhpNsq:

php.CRITICAL: Class 'OkStuff\PhpNsq\Utility\Logging' not found {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Class 'OkStuff\\PhpNsq\\Utility\\Logging' not found at /data/projects/...vendor/okstuff/phpnsq/src/phpnsq/PhpNsq.php:25)"} []

I've tried using alias as

use OkStuff\PhpNsq\Utility\Logging;

and changed a path:

"logdir" => "/tmp",

that has really full write access. Nothing helps.

On vendor if I comment a code:

    public function __construct($nsq)
    {
        $this->reader = new reader();
        $this->logger = new Logging("PHPNSQ", $nsq["nsq"]["logdir"]);
        $this->pool   = new Pool($nsq);
    }

It occured another errors from instance Pool.

stream_select failed, fclose() supplied resource is not a valid stream source in Tunnel.php

When consumer exit with exception, i found pfsockopen produced unexpected behavior when send new message to nsq in consumer context. Change pfsockopen to fsockopen, it works correct. My code depends on old version 1.0.1 (with composer install)๏ผŒcan't upgrade new version because php runtime version, and can't send pull request to your project based version 1.0.1, can you help me to fix it?

Change fpsockopen to fsockopen in src/phpnsq/Utility/Stream.php on tag 1.0.1 and then tag it 1.0.2 .
image

Thank you !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.