goalgorilla/graphql-php-ws
Composer 安装命令:
composer require goalgorilla/graphql-php-ws
包简介
GraphQL WebSocket middleware for Ratchet.
README 文档
README
A PHP implementation of the GraphQL over WebSocket Protocol using Ratchet.
Work in Progress
This library is in active development and its interfaces, implementation and usage is bound to change. Don't use this if you're not willing to invest the time to rewrite the application you build on top of it.
Usage
<?php use GraphQL\Language\AST\OperationDefinitionNode; use GraphQLWs\GraphQLWsSubscriberInterface; use GraphQLWs\GraphQLWsSubscriptionServer; use Ratchet\Http\HttpServer; use Ratchet\Server\IoServer; use Ratchet\WebSocket\WsConnection; use Ratchet\WebSocket\WsServer; use React\EventLoop\Factory; use React\Socket\Server as Reactor; use Symfony\Component\HttpKernel\Log\Logger; class RedisEventQueue implements GraphQLWsSubscriberInterface { /** * The subscriptions listening to our GraphQL */ protected array $subscriptions = []; /** * Called with new subscribers. * * {@inheritdoc} */ public function onSubscribe(string $subscription_id, WsConnection $client, OperationDefinitionNode $query, ?string $operationName = NULL, ?array $variables = NULL) : void{ $this->subscriptions[$subscription_id] = [ 'client' => $client, 'query' => $query, 'operationName' => $operationName, 'variables' => $variables, ]; } /** * Called when connections are closed. * * {@inheritdoc} */ public function onComplete(string $subscription_id) : void{ unset($this->subscriptions[$subscription_id]); } /** * Example function receiving data from an event system (e.g. a Redis queue). */ public function onData($data) { // For this example we simply naïvely write the data to the client. This is // most certainly a GraphQL error. This is where you'd actually resolve the // queries your subscribers are subscribed to with the new data. This can be // made easier by doing some double bookkeeping in the onSubscribe event // handler but that's out of the scope of this example. foreach ($this->subscriptions as $subscription) { $subscription['client']->send(json_encode($data)); } } } $ws_address = "localhost:8000"; $logger = new Logger(); $event_loop = Factory::create(); // Something that receives new data from an external system and keeps track of // subscriptions. Any new data is sent to the subscriptions that have asked for // the data. $subscription_handler = new RedisEventQueue($event_loop); // Set up the Websocket server. It requires an event loop to handle some // timeouts that are part of the GraphQL WS Protocol. $subscription_server = new GraphQLWsSubscriptionServer($logger, $event_loop); $subscription_server->addEventHandler($subscription_handler); // Set up our actual stack that handles HTTP and WebSockets. The server above is // only the protocol handler itself. $ws_app = new HttpServer(new WsServer($subscription_server)); $ws_socket = new Reactor($ws_address, $this->eventLoop); new IoServer($ws_app, $ws_socket, $this->eventLoop); $logger->info( "Listening for WebSocket connections on ws://{address}", ["address" => $ws_address] ); // Kick-off the React event loop to start our server. $event_loop->run();
goalgorilla/graphql-php-ws 适用场景与选型建议
goalgorilla/graphql-php-ws 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128.21k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 goalgorilla/graphql-php-ws 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 goalgorilla/graphql-php-ws 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 128.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-16