承接 amarwave/amarwave-laravel 相关项目开发

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

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

amarwave/amarwave-laravel

Composer 安装命令:

composer require amarwave/amarwave-laravel

包简介

Laravel service provider, facade, and broadcasting driver for AmarWave real-time messaging.

README 文档

README

Laravel service provider, facade, and broadcasting driver for AmarWave real-time messaging.

This package wraps amarwave/amarwave-php and wires it into the Laravel framework.

Requirements

  • PHP 8.1+
  • Laravel 10, 11, or 12

Installation

composer require amarwave/amarwave-laravel

Laravel auto-discovers the service provider and facade. No manual registration needed.

Configuration

Publish the config file:

php artisan vendor:publish --tag=amarwave-config

Add to your .env:

AMARWAVE_APP_KEY=your-app-key
AMARWAVE_APP_SECRET=your-app-secret
AMARWAVE_CLUSTER=default
AMARWAVE_TIMEOUT=10

Usage

Facade

use AmarWave\Laravel\AmarWave;

AmarWave::trigger('orders', 'placed', ['order_id' => 42]);

AmarWave::triggerBatch([
    ['channel' => 'chat-1', 'event' => 'message', 'data' => ['text' => 'Hello']],
    ['channel' => 'chat-2', 'event' => 'message', 'data' => ['text' => 'World']],
]);

Or with the global alias:

\AmarWave::trigger('orders', 'placed', ['order_id' => 42]);

Dependency Injection

use AmarWave\AmarWave;

class OrderController extends Controller
{
    public function __construct(private readonly AmarWave $aw) {}

    public function store(Request $request): JsonResponse
    {
        $order = Order::create($request->validated());
        $this->aw->trigger('orders', 'placed', $order->toArray());
        return response()->json($order, 201);
    }
}

Broadcasting Driver

Use AmarWave as a Laravel broadcasting driver so broadcast(new YourEvent) works out of the box.

1 — Add the connection to config/broadcasting.php

'connections' => [
    'amarwave' => [
        'driver' => 'amarwave',
    ],
],

2 — Set the driver in .env

# Laravel 10
BROADCAST_DRIVER=amarwave
# Laravel 11+
BROADCAST_CONNECTION=amarwave

3 — Create a broadcastable event

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class OrderPlaced implements ShouldBroadcast
{
    public function __construct(public readonly array $order) {}

    public function broadcastOn(): array
    {
        return [
            new Channel('orders'),
            new PrivateChannel("user.{$this->order['user_id']}"),
        ];
    }

    public function broadcastAs(): string { return 'order.placed'; }

    public function broadcastWith(): array
    {
        return ['order_id' => $this->order['id'], 'total' => $this->order['total']];
    }
}

4 — Dispatch

broadcast(new OrderPlaced($order));

Channel Authorization

In routes/channels.php:

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('user.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('presence-room.{roomId}', function ($user, $roomId) {
    $room = \App\Models\Room::find($roomId);
    if ($room?->members()->where('user_id', $user->id)->exists()) {
        return ['id' => $user->id, 'name' => $user->name];
    }
    return false;
});

License

MIT

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固