zaeem2396/laravel-nats 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zaeem2396/laravel-nats

Composer 安装命令:

composer require zaeem2396/laravel-nats

包简介

Laravel NATS - v2 basis stack (basis-company/nats), subscriber, NatsV2 JetStream helpers, queue driver, legacy JetStream, Artisan commands

README 文档

README

Tests Static Analysis Code Style Pint

Laravel wrapper for NATS with two tracks and an index-first docs experience:

  • Recommended: NatsV2 on basis-company/nats
  • Supported legacy track: Nats facade + legacy queue/JetStream APIs

Quick Navigation

What To Use

NatsV2 (recommended)

Use NatsV2 for new code:

  • NatsV2::publish / NatsV2::subscribe
  • basis-client JetStream helpers
  • nats_basis queue driver
  • optional idempotency, observability, and security controls

Legacy (still supported)

Use legacy Nats APIs only when you must keep existing workloads unchanged. Migration guide: docs/v2/MIGRATION.md.

Install

Requirements:

  • PHP 8.2+
  • Laravel 10.x / 11.x / 12.x / 13.x
  • NATS Server 2.x

Laravel 13 applications require PHP 8.3+; the package minimum remains PHP 8.2 for Laravel 10-12.

composer require zaeem2396/laravel-nats
php artisan vendor:publish --tag=nats-config

Version map:

  • 1.3.0+: NatsV2 publish/subscribe foundation
  • 1.4.0+: basis JetStream + nats_basis queue + idempotency + observability
  • 1.5.0+: config validation, TLS production guard, optional ACL
  • 1.5.1: documentation refresh (navigation + cross-links)
  • 1.5.2: README/docs index layout; Docker local NATS section; docs/INDEX.md
  • 1.6.0+: v2.7 header helpers, optional W3C trace context, subject-prefix connection selection, outbox recipe; Laravel 13 supported in package dev constraints; CI tests PHP 8.5 on Laravel 11–13
  • 1.6.1: expanded Pest coverage, Pint CI workflow, composer ci script; no public API changes
  • 1.6.2: connection reconnect helpers and transport cleanup for legacy and basis clients

Roadmap: docs/ROADMAP.md · Changelog: CHANGELOG.md

Docker Setup (Local NATS)

Start NATS with project compose file

docker compose up -d

This starts a local NATS server using the repository's docker-compose.yml.

Verify container is running

docker compose ps

Match your .env to local Docker defaults

NATS_HOST=127.0.0.1
NATS_PORT=4222

Stop services when done

docker compose down

5-Minute Quickstart (NatsV2)

1) Configure env

NATS_HOST=127.0.0.1
NATS_PORT=4222
NATS_USER=
NATS_PASS=
NATS_TOKEN=
NATS_BASIS_VALIDATE_CONFIG=false
NATS_TLS_REQUIRE_IN_PRODUCTION=false

2) Publish

use LaravelNats\Laravel\Facades\NatsV2;

NatsV2::publish('orders.created', ['order_id' => 123], ['X-Request-Id' => 'req-1']);

3) Subscribe

use LaravelNats\Laravel\Facades\NatsV2;
use LaravelNats\Subscriber\InboundMessage;

NatsV2::subscribe('orders.created', function (InboundMessage $message): void {
    $payload = $message->envelopePayload();
    logger()->info('Order received', (array) $payload);
});

while (true) {
    NatsV2::process(null, 1.0);
}

Documentation Index

Prefer this canonical map for quick navigation: docs/INDEX.md.

By use case

If you want to... Go here
Publish/subscribe on v2 quickly docs/v2/GUIDE.md
Build long-running consumers docs/v2/SUBSCRIBER.md
Run queue workers on basis client docs/v2/QUEUE.md
Work with JetStream streams/consumers docs/v2/JETSTREAM.md
Propagate trace headers or route by subject docs/v2/TRACE_CONTEXT.md, docs/v2/CONNECTION_SELECTION.md
Add a transactional outbox around publishes docs/v2/OUTBOX.md
Harden config and subject access docs/v2/SECURITY.md
Move from legacy APIs to v2 docs/v2/MIGRATION.md

Start here

Core feature guides

Production and operations

Examples

Queue Usage

Two queue drivers are available:

  • nats: legacy driver on LaravelNats\Core\Client
  • nats_basis: recommended driver on Basis\Nats\Client

Use with Laravel worker:

php artisan queue:work nats_basis --queue=default --tries=3

Queue guide: docs/v2/QUEUE.md

Legacy Command References

For teams still using legacy stack workflows:

Security And Production Checks

For hardened environments (1.5.0+):

  • optional boot validation: NATS_BASIS_VALIDATE_CONFIG=true
  • optional TLS requirement in production: NATS_TLS_REQUIRE_IN_PRODUCTION=true
  • optional publish/subscribe ACL: NATS_ACL_*
  • explicit validator command: php artisan nats:v2:config:validate

Full details: docs/v2/SECURITY.md

Testing And Quality Checks

# Optional local NATS (integration tests)
docker compose up -d

# Full local CI gate (Pest + PHPStan + Pint + PHP-CS-Fixer)
composer ci

# Individual checks
composer test
composer analyse
composer pint:check
composer format:check
composer test:coverage   # optional; requires PCOV or Xdebug

For release prep, run composer ci and ensure CHANGELOG.md reflects the release scope.

Troubleshooting

Connection refused

Start NATS locally:

docker compose up -d

Authorization violation

Verify .env credentials and server auth mode (NATS_USER/NATS_PASS vs NATS_TOKEN).

Production TLS/config errors

If you get NatsConfigurationException, validate your nats_basis connection rows:

php artisan nats:v2:config:validate

Then confirm TLS values in docs/v2/SECURITY.md.

API Stability

Public Laravel-facing APIs are stable under semver. Internal implementation classes (especially under core internals) may evolve.

See release notes: CHANGELOG.md

Contributing

Before opening a PR, run:

composer ci

License

MIT. See LICENSE.

zaeem2396/laravel-nats 适用场景与选型建议

zaeem2396/laravel-nats 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 455 次下载、GitHub Stars 达 6, 最近一次更新时间为 2026 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「queue」 「messaging」 「laravel」 「pub-sub」 「nats」 「microservices」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zaeem2396/laravel-nats 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zaeem2396/laravel-nats 我们能提供哪些服务?
定制开发 / 二次开发

基于 zaeem2396/laravel-nats 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 455
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-04