behzadsh/rabbitmq-broadcaster
Composer 安装命令:
composer require behzadsh/rabbitmq-broadcaster
包简介
A laravel event broadcaster which broadcasts event into a fanout or topic RabbitMQ exchange.
README 文档
README
The main focus of Laravel's event broadcasting module is to enable communication with the client-side of the application. However, it can also be useful for asynchronous communication and message passing between services, especially in a microservices architecture.
This package provides an easy way to broadcast Laravel events to a RabbitMQ server, enabling you to send messages asynchronously and facilitating communication between services in a microservices architecture.
Installation
To install the package, run the following command:
composer require behzadsh/rabbitmq-broadcaster
After installing the package, append the broadcaster config as shown in the snippet below to your broadcasting.php
config file:
<?php return [ // ... 'connections' => [ // other connections config 'rabbitmq' => [ 'driver' => 'rabbitmq', 'connection' => [ 'host' => env('RABBITMQ_HOST', 'localhost'), 'port' => env('RABBITMQ_PORT', 5672), 'user' => env('RABBITMQ_USER', 'guest'), 'password' => env('RABBITMQ_PASSWORD', 'guest'), ], 'default_exchange_type' => 'fanout', // It can be `fanout` or `topic` 'exchange_configs' => [ // optional 'exchange1' => [ 'type' => 'fanout', 'passive' => false, 'durable' => true, 'auto_delete' => false, 'internal' => false, ], 'exchange2' => [ 'type' => 'topic', 'passive' => false, 'durable' => false, 'auto_delete' => true, 'internal' => false, ], ], ], ], ];
Then, append the package service provider at the end of the providers list in your app.php config file:
<?php return [ // ... 'providers' => [ // Other service providers... Behzadsh\RabbitMQBroadcaster\RabbitMQBroadcasterServiceProvider::class, ] // ... ];
Usage
To use the RabbitMQ event broadcaster, your desired event class must implement the
Illuminate\Contracts\Broadcasting\ShouldBroadcast interface, which requires you to implement the broadcastOn method.
The broadcastOn method should return a channel or an array of channels that the event should broadcast on.
Since this package is primarily intended for communicating between internal backend services, channels are not public,
and access to the channels from client-side applications is not allowed. Therefore, it's recommended that you avoid using
the PrivateChannel, PresenceChannel, and EncryptedPrivateChannel and use a simple Channel instead.
<?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class UserRegistered implements ShouldBroadcast { /** * Create a new event instance. */ public function __construct( public int $userId, public string $name, public string $email, ) {} /** * Get the channels the event should broadcast on. * * @return array<int, \Illuminate\Broadcasting\Channel> */ public function broadcastOn(): array { return [ new Channel('user.events'), ]; } }
It is recommended to implement the broadcastWith method if you construct your event with an Eloquent Model. The
broadcastWith method should return an array that can be serialized into JSON.
<?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Queue\SerializesModels; class ServerCreated implements ShouldBroadcast { use SerializesModels; /** * Create a new event instance. */ public function __construct( public User $user, ) {} /** * Get the channels the event should broadcast on. * * @return array<int, \Illuminate\Broadcasting\Channel> */ public function broadcastOn(): array { return [ new Channel('user.events'), ]; } /** * Get the data to broadcast. * * @return array<string, mixed> */ public function broadcastWith(): array { return [ 'userId' => $this->user->id, 'name' => $this->user->name, 'email' => $this->user->email, ]; } }
behzadsh/rabbitmq-broadcaster 适用场景与选型建议
behzadsh/rabbitmq-broadcaster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.2k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「events」 「rabbitmq」 「laravel」 「Broadcast」 「fanout」 「message-broker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 behzadsh/rabbitmq-broadcaster 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 behzadsh/rabbitmq-broadcaster 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 behzadsh/rabbitmq-broadcaster 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Work with RabbitMQ in Laravel.
Microservice RPC through message queues.
PHP AMQP Binding Library
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
A powerful event calendar Tool for Laravel's Nova 5.
统计信息
- 总下载量: 3.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-23