katzen48/laravel-twitch-eventsub
Composer 安装命令:
composer require katzen48/laravel-twitch-eventsub
包简介
This package provides support for subscribing to Twitch EventSub. It's based on laravel-twitch and fully compatible.
README 文档
README
Introduction
This package provides support for subscribing to Twitch EventSub. It's based on laravel-twitch and fully compatible.
Features
- Create Twitch EventSub subscriptions via webhooks
- Subscribe to the received events on a per-event basis through the dedicated classes
- Define events, to subscribe to, whenever a specific model is saved to the database
Installation
First, install laravel-twitch.
Please refer to their documentation.
After laravel-twitch is installed and configured, install laravel-twitch-eventsub from composer
composer require katzen48/laravel-twitch-eventsub
The config can be published with
php artisan vendor:publish --tag=config
Configuration
After you published the config, you can find it in config/twitch-eventsub.php and it looks like this:
<?php return [ 'callback_url' => env('TWITCH_HELIX_EVENTSUB_CALLBACK_URL', '/twitch/eventsub/webhook'), // Endpoint, the webhooks get sent to ];
Consume EventSub events
Even when you don't subscribe to eventsub using this library, a route is registered, which
defaults to {APP_URL}/twitch/eventsub/webhook. When you subscribe to EventSub and use this URL as a callback,
received events are parsed into dedicated events classes, which event listeners can subscribed to, individually.
To subscribe to an event, first create a listener with php artisan make:listener <Name> and add the event it should
subscribe to as a parameter to the handle() function.
<?php namespace App\Listeners; use katzen48\Twitch\EventSub\Events\Channel\Subscription\ChannelSubscriptionGiftEvent; class SubscriptionListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param object $event * @return void */ public function handle(ChannelSubscriptionGiftEvent $event) { // Do something } }
To bind the listener to an event, add the listener to the $listen array in your EventServiceProvider
<?php namespace App\Providers; use App\Listeners\SubscriptionListener;use Illuminate\Auth\Events\Registered;use Illuminate\Auth\Listeners\SendEmailVerificationNotification;use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;use katzen48\Twitch\EventSub\Events\Channel\Subscription\ChannelSubscriptionGiftEvent; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], ChannelSubscriptionGiftEvent::class => [ // The Event, to subscribe to SubscriptionListener::class, // The Listener ], ]; /** * Register any events for your application. * * @return void */ public function boot() { // } }
There you go.
Your listener now gets incoming events of type channel.subscribe from the Twitch EventSub.
Subscribe to Twitch EventSub on model save
Sometimes it is necessary, that newly created instances of a model trigger a subscription to the Twitch EventSub
e.g. a User logged in for the first time.
This behavior can be automated with the static SubscribesEventSubs trait.
Add the trait SubscribesEventSubs to your model.
Next add a static variable called $eventSubs to your model with the events to subscribe to and the conditions,
mapped to the attributes of your model.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use katzen48\Twitch\EventSub\Traits\SubscribesEventSubs; use romanzipp\Twitch\Enums\EventSubType; class User extends Authenticatable { use HasFactory, Notifiable, SubscribesEventSubs; public static array $eventSubs = [ EventSubType::CHANNEL_UPDATE => [ // The Event Type (from laravel-twitch) 'broadcaster_user_id' => 'id', // The conditions from the EventSub documentation and the model attributes ], ]; protected $keyType = 'string'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'id', 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'id' => 'string', 'email_verified_at' => 'datetime', ]; }
Now, whenever the created event is fired on that model, a subscription to the channel.update event tyoe is made.
The id property of the model is automatically inserted for broadcaster_user_id.
katzen48/laravel-twitch-eventsub 适用场景与选型建议
katzen48/laravel-twitch-eventsub 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.04k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 katzen48/laravel-twitch-eventsub 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 katzen48/laravel-twitch-eventsub 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-10