danilopolani/twitch-pub-sub 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

danilopolani/twitch-pub-sub

Composer 安装命令:

composer require danilopolani/twitch-pub-sub

包简介

Integrate Twitch PubSub Web Sockets with Laravel

README 文档

README

Latest Version on Packagist Build Status


Laravel Twitch PubSub

Connect to Twitch PubSub (Web Sockets) in a Laravel application, dispatching Events whenever a message for a topic is received.

Built with Amphp with Web Socket.

Table of Contents

  1. Getting Started
  2. Usage
  3. Changelog
  4. Contributing
  5. Testing
  6. Security
  7. Credits
  8. License

Getting Started

The package supports Laravel 8.x and PHP >= 7.4.

Prerequisites

The PHP extension ext-pcntl is required.

Installation

You can install the package via composer:

composer require danilopolani/twitch-pub-sub

Usage

The package relies on one main function:

TwitchPubSub::run(string|array $twitchAuthToken, array $topics = [])
Argument Description
array|string $twitchAuthToken if string, it must be a valid Auth Token, otherwise it can be an associative array of authToken => topics[]
array $topics an array of valid topics, needed only if $twitchAuthToken is a string

Usually you would put the main function of the package inside an Artisan Command.

use \Danilopolani\TwitchPubSub\Facades\TwitchPubSub;

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    TwitchPubSub::run('a1b2c3d4e5', ['whispers.44322889']);

    // Or the array syntax that support multiple users too
    TwitchPubSub::run([
        'a1b2c3d4e5' => ['whispers.44322889'],
        'f6g7h8j9k0' => ['channel-bits-events-v1.123456', 'channel-points-channel-v1.123456'],
    ]);
}

Now you can run your command from your terminal or a worker.

You should definitely setup Supervisor or a similar tool to keep your command alive and restart it if something goes wrong.

Finally, create a Listener to handle the incoming events.

// App\Providers\EventServiceProvider.php

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    \Danilopolani\TwitchPubSub\Events\WhisperReceived::class => [
        TrackMessages::class,
    ],
];

// Or with a closure
Event::listen(function (\Danilopolani\TwitchPubSub\Events\WhisperReceived $event) {
    dd($event->data);
});

Topics & Events

Topic Event
channel-bits-events-v1.<channel_id> \Danilopolani\TwitchPubSub\Events\BitsDonated
channel-bits-events-v2.<channel_id> \Danilopolani\TwitchPubSub\Events\BitsDonated
channel-bits-badge-unlocks.<channel_id> \Danilopolani\TwitchPubSub\Events\BitsBadgeUnlocked
channel-points-channel-v1.<channel_id> \Danilopolani\TwitchPubSub\Events\RewardRedeemed
channel-subscribe-events-v1.<channel_id> \Danilopolani\TwitchPubSub\Events\SubscriptionReceived
chat_moderator_actions.<user_id>.<channel_id> \Danilopolani\TwitchPubSub\Events\ModeratorActionSent
whispers.<user_id> \Danilopolani\TwitchPubSub\Events\WhisperReceived

Reconnection

When the connection is closed, the package itself will try to attempt a reconnection, but this would need a fresh access token, furthermore we strongly suggest you to handle the onClose callback and exit your script. This, with a correct configuration of Supervisor, will restart the worker automatically reconnecting with a fresh token, if your code is written in that way. Below a simple example with a correct flow to demonstrate how it should work:

// App/Console/Commands/PubSub.php

public function handle()
{
    // A fresh Twitch Access Token
    $token = $user->getFreshAccessToken();
    
    TwitchPubSub::onClose(function (\Amp\Websocket\ClosedException $e) {
        exit(0);
    });
    
    TwitchPubSub::run($token, ['my-topic']);
}

When exit(0) will be executed, the script will stop, Supervisor will restart it - invoking handle again - and refreshing the token reconnecting correctly.
Please see below for more information about callbacks.

Callbacks

The package provides several callbacks fired when something occurs. These callbacks must be put before the ::run() method to let them work correctly.

// A message (anything, PING/PONG too for example) is received
TwitchPubSub::onMessage(function (array $payload) {
    dump('received message:', $payload);
});

// A generic error occurs
TwitchPubSub::onError(function (\Exception $e) {
    dump('generic error:', $e->getMessage());
});

// The connection has been closed
// This could triggered from a SIGINT or SIGTERM too (stopping the script, restarting the worker etc.)
TwitchPubSub::onClose(function (\Amp\Websocket\ClosedException $e) {
    dump('connection closed, reason:', $e->getMessage());
});

// An error occurred in a Listener after the event has been dispatched
TwitchPubSub::onDispatchError(function (string $event, array $payload, Throwable $e) {
    dump('error for event', $event, $payload, $e->getMessage());
});

// Runner
TwitchPubSub::run('a1b2c3d4e5', ['whispers.44322889']);

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Testing

Clone the repository and just run

composer test

With Docker (Windows):

docker run --rm -v %cd%:/app composer:2 bash -c "cd /app && composer install --ignore-platform-reqs && ./vendor/bin/phpunit"

With Docker (Linux/OSX):

docker run --rm -v $(pwd):/app composer:2 bash -c "cd /app && composer install --ignore-platform-reqs && ./vendor/bin/phpunit"

Security

If you discover any security related issues, please email danilo.polani@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

danilopolani/twitch-pub-sub 适用场景与选型建议

danilopolani/twitch-pub-sub 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 danilopolani/twitch-pub-sub 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-13