承接 friendsofhyperf/ipc-broadcaster 相关项目开发

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

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

friendsofhyperf/ipc-broadcaster

Composer 安装命令:

composer require friendsofhyperf/ipc-broadcaster

包简介

IPC broadcaster component for Hyperf.

关键字:

README 文档

README

中文说明

Broadcast serializable messages between Hyperf server workers and user processes.

Installation

composer require friendsofhyperf/ipc-broadcaster

The package automatically registers its ConfigProvider. It binds BroadcasterInterface to AllProcessesBroadcaster and registers the listeners required to receive messages in server workers. There is no configuration file to publish.

The package declares hyperf/event ~3.2.0 and no optional dependencies in its composer.json. Use it in a Hyperf server application that provides the server, process, container, and DI runtime classes used by the broadcasters.

Broadcast Messages

The broadcast() function accepts an IpcMessageInterface instance or a closure. By default, it uses AllProcessesBroadcaster to send the message to all other server workers and all registered coroutine user processes.

Class Message

Extend IpcMessage and implement handle(). IpcMessage also provides getFromWorkerId() and setFromWorkerId() through InteractsWithFromWorkerId. When a server worker receives the message, getFromWorkerId() contains the sending worker ID.

<?php

namespace App\Broadcasting;

use FriendsOfHyperf\IpcBroadcaster\IpcMessage;

use function FriendsOfHyperf\IpcBroadcaster\broadcast;

class FooMessage extends IpcMessage
{
    public function __construct(private string $foo)
    {
    }

    public function handle(): void
    {
        echo $this->foo;
    }
}

broadcast(new FooMessage('bar'));

Message objects and their properties must be serializable so they can cross process boundaries.

Closure Message

Closures are wrapped in ClosureIpcMessage and serialized before broadcasting. Values captured by the closure must therefore also be serializable. In a normal Hyperf DI environment, typed closure parameters can be resolved from the container.

use function FriendsOfHyperf\IpcBroadcaster\broadcast;

broadcast(function () {
    echo 'Hello world';
});

Select Targets

Inject BroadcasterInterface for the default all-processes behavior, or construct a specific broadcaster to select targets:

use FriendsOfHyperf\IpcBroadcaster\ServerBroadcaster;
use FriendsOfHyperf\IpcBroadcaster\UserProcessesBroadcaster;

$serverBroadcaster = new ServerBroadcaster($container, id: 1);
$serverBroadcaster->broadcast($message);

$userProcessBroadcaster = new UserProcessesBroadcaster(name: 'reporting', id: 0);
$userProcessBroadcaster->broadcast($message);

ServerBroadcaster accepts a container and an optional worker ID. Without an ID, it sends to every server worker except the current worker. UserProcessesBroadcaster accepts an optional process name and process ID. Without either, it sends to all registered user processes collected by Hyperf's ProcessCollector.

Handle Messages in User Processes

The component automatically calls handle() for messages received by server workers. In a user process, Hyperf dispatches a Hyperf\Process\Event\PipeMessage; the component does not automatically call the contained message's handle() method. Register a listener when the user process should execute the message:

use FriendsOfHyperf\IpcBroadcaster\Contract\IpcMessageInterface;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Process\Event\PipeMessage;

class UserProcessPipeMessageListener implements ListenerInterface
{
    public function listen(): array
    {
        return [PipeMessage::class];
    }

    public function process(object $event): void
    {
        if ($event instanceof PipeMessage && $event->data instanceof IpcMessageInterface) {
            $event->data->handle();
        }
    }
}

Run in the Current Worker

By default, broadcasts do not execute in the current server worker. Add RunsInCurrentWorker to a message to call handle() once in the current worker before it is sent to the selected targets:

use FriendsOfHyperf\IpcBroadcaster\IpcMessage;
use FriendsOfHyperf\IpcBroadcaster\Traits\RunsInCurrentWorker;

class RefreshMessage extends IpcMessage
{
    use RunsInCurrentWorker;

    public function handle(): void
    {
        // Refresh local state.
    }
}

Coroutine Server Limitation

After MainCoroutineServerStart, cross-process broadcasting is disabled and broadcaster calls return without sending messages. A message using RunsInCurrentWorker still runs once locally before the broadcaster returns.

friendsofhyperf/ipc-broadcaster 适用场景与选型建议

friendsofhyperf/ipc-broadcaster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.24k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 friendsofhyperf/ipc-broadcaster 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-26