How to Install and Use NSQ with PHP-NSQ: A Step-by-Step Guide
This guide explains what NSQ is, shows how to install NSQ via Docker, set up the PHP‑NSQ extension by installing libevent and the PECL package, and provides complete PHP code examples for publishing, deferred publishing, and subscribing to messages.
NSQ Overview
NSQ is a distributed, real‑time messaging platform designed for high‑throughput, fault‑tolerant, and highly available message delivery in decentralized services. It can handle hundreds of millions of messages per day and supports many protocols.
PHP‑NSQ Client
PHP‑NSQ is a PHP 7/8 extension that enables PHP applications to interact with NSQ, providing publish and subscribe capabilities similar to Kafka.
Installing NSQ with Docker
$ docker pull nsqio/nsq
Using default tag: latest
latest: Pulling from nsqio/nsq
709515475419: Pull complete
efd1c5a69d15: Pull complete
fa61d00bb52d: Pull complete
Digest: sha256:fad1937a88fec5b66fb9f4837b72ad3b70012692826aed5c6435f93c5a23b690
Status: Downloaded newer image for nsqio/nsq:latestAfter the container starts, you can view the NSQ admin UI (image omitted) and publish a test message:
curl -d 'Hello Linux localhost Msg' 'http://127.0.0.1:4151/pub?topic=test'The message can be inspected via the NSQ dashboard (image omitted).
Installing PHP‑NSQ Extension
Install libevent
The extension depends on the libevent library. Install it with your system’s package manager:
# Debian/Ubuntu
apt-get install libevent-dev
# CentOS/RHEL
yum install libevent-devel
# macOS
brew install libeventInstall the NSQ PECL package
pecl install nsqPHP Usage Example
<?php
namespace app\index\controller;
class NsqController extends Controller
{
public function index()
{
ini_set('memory_limit', '8M');
$nsqdAddr = ["127.0.0.1:4151", "127.0.0.1:4150"];
$nsq = new \Nsq();
$isTrue = $nsq->connectNsqd($nsqdAddr);
for ($i = 0; $i < 6; $i++) {
$nsq->publish("test", "Hi Tinywan");
}
$nsq->closeNsqdConnection();
halt($isTrue);
// Deferred publish example
$deferred = new \Nsq();
$isTrue = $deferred->connectNsqd($nsqdAddr);
for ($i = 0; $i < 20; $i++) {
$deferred->deferredPublish("test", "message daly", 3000);
}
$deferred->closeNsqdConnection();
}
public function nsqSubMessage()
{
$nsq_lookupd = new \NsqLookupd("127.0.0.1:4161");
$nsq = new \Nsq();
$config = array(
"topic" => "test",
"channel" => "struggle",
"rdy" => 2, // optional, default 1
"connect_num" => 1, // optional, default 1
"retry_delay_time" => 5000,// optional, default 0
"auto_finish" => true, // default true
);
$nsq->subscribe($nsq_lookupd, $config, function($msg, $bev) {
echo $msg->payload . "
";
echo $msg->attempts . "
";
echo $msg->messageId . "
";
echo $msg->timestamp . "
";
});
}
}Running the controller from the command line (e.g., php think pay nsq) prints connection status and received messages, as shown in the console output (image omitted).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
