peal-ihertz/laravel-nats
Composer 安装命令:
composer require peal-ihertz/laravel-nats
包简介
Laravel integration for basis-company/nats (NATS JetStream client)
README 文档
README
Effortless real-time communication for Laravel apps using NATS.io via basis-company/nats.php
🧩 Overview
Laravel NATS is a wrapper package that allows Laravel developers to interact easily with the NATS messaging system.
NATS (Neural Autonomic Transport System) is a high-performance, lightweight messaging system for microservices, IoT, and distributed systems.
With this package, you can:
- 📤 Publish messages to NATS subjects
- 📩 Subscribe to messages
- 🔁 Request/Reply pattern using callbacks
- ⚙️ Integrate seamlessly within controllers, commands, or queued jobs
- 🧠 Use Laravel Facades, Service Container bindings, and Configuration
- 🧪 Test easily using PestPHP
⚙️ Installation
composer require peal-ihertz/laravel-nats dev-main
🧰 Configuration
Publish the config file (optional):
php artisan vendor:publish --tag=config
This will create:
// config/nats.php return [ 'host' => env('NATS_HOST', '127.0.0.1'), 'port' => env('NATS_PORT', 4222), ];
You can connect your Dockerized NATS instance or remote cluster:
NATS_HOST=nats NATS_PORT=4222
🧱 Usage
🔹 Publish a Message
use Nats; Nats::publish('notifications.new', ['message' => 'New user registered']);
or manually with a payload:
use Basis\Nats\Message\Payload; $payload = new Payload(json_encode(['id' => 123])); Nats::publish('orders.created', $payload);
🔹 Subscribe to a Subject
use Nats; Nats::subscribe('notifications.new', function ($msg) { $data = json_decode($msg->body, true); logger('New notification received:', $data); });
🔹 Request/Reply (Callback Pattern)
NATS supports RPC-style communication through a request-reply mechanism.
Client Side:
Nats::request('math.add', ['a' => 5, 'b' => 3], function ($response) { $data = json_decode($response->body, true); logger('Response from server:', $data); });
Server Side (Subscriber):
Nats::subscribe('math.add', function ($msg) { $data = json_decode($msg->body, true); $sum = $data['a'] + $data['b']; $msg->reply(json_encode(['sum' => $sum])); });
✅ The callback executes once the reply arrives — making it ideal for interactive real-time systems.
🔹 Example Controller
use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Nats; class NatsController extends Controller { public function send() { Nats::publish('notifications.new', ['event' => 'UserRegistered']); return response()->json(['message' => 'Notification sent!']); } public function request() { Nats::request('math.add', ['a' => 10, 'b' => 7], function ($response) { logger('Received reply:', [json_decode($response->body, true)]); }); return response()->json(['status' => 'request sent']); } }
🧠 Advanced Features
1. JetStream Support (Optional)
If you use NATS JetStream, you can extend this package to handle durable streams and message persistence.
The underlying client supports JetStream commands and metadata.
2. Durable Subscriptions
Persistent subscriptions let you replay missed messages and process them at-least-once.
3. Async Handling
NATS supports fully asynchronous publish/subscribe and request/reply patterns.
4. Clustered & Secure
You can connect to multiple NATS servers, use TLS, and authentication via NATS tokens.
🧪 Testing
Running Tests
This package uses PestPHP.
Run all tests:
./vendor/bin/pest
Feature tests example (tests/Feature/NatsPublishTest.php):
it('can publish messages to nats', function () { $client = Mockery::mock(\Basis\Nats\Client::class); $client->shouldReceive('publish')->once(); app()->instance(\Basis\Nats\Client::class, $client); Nats::publish('test.subject', ['foo' => 'bar']); });
🐳 Example Docker Compose (for NATS testing)
version: "3" services: nats: image: nats:latest ports: - "4222:4222" - "8222:8222"
Run:
docker-compose up -d
💡 Why Use NATS with Laravel?
| Feature | Description |
|---|---|
| ⚡ Speed | Handles millions of messages per second |
| 🧩 Simplicity | Easy subject-based routing |
| 🔁 Request/Reply | Built-in microservice communication |
| ☁️ Scalable | Cluster and stream messages easily |
| 🧠 Lightweight | Zero dependencies for core message broker |
| 🔐 Secure | TLS and user authentication supported |
🧾 License
MIT License © 2025 Mohammed Minuddin Peal - iHERTZ Technology
peal-ihertz/laravel-nats 适用场景与选型建议
peal-ihertz/laravel-nats 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 peal-ihertz/laravel-nats 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 peal-ihertz/laravel-nats 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-18