承接 behzadsh/rabbitmq-broadcaster 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-23