PHP code example of pecherskiy-v / phpnsq

1. Go to this page and download the library: Download pecherskiy-v/phpnsq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pecherskiy-v / phpnsq example snippets



OkStuff\PhpNsq\PhpNsq;

$config = l 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);



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) {
                $phpnsq->getLogger()->info("READ", [$message]);
            });
        //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();
    }
}

#!/usr/bin/env php


nsole\Application;
use OkStuff\PhpNsq\Cmd\Subscribe;

$application = new Application();

$config =