GraphQL 进阶: 使用RabbitMQ做为PubSub后端实现GraphQL订阅

这篇短文源于解决版本兼容性问题. graphql-rabbitmq-subscriptions 这项目已经半年没有Commit了, 和最新的 graphql-subscriptions 有兼容性问题. 最新的 graphql-subscriptions 的GraphQL订阅使用了 AsyncIterator, PubSubEngine增长了一个新的接口asyncIterator, 以下:git

export interface PubSubEngine {
  publish(triggerName: string, payload: any): boolean;
  subscribe(triggerName: string, onMessage: Function, options: Object): Promise<number>;
  unsubscribe(subId: number): any;
  asyncIterator<T>(triggers: string | string[]): AsyncIterator<T>;
}

如何运行

clipboard.png

左边开两个GraphiQL的窗口, 右侧登陆 rabbitmqadmin 图形管理界面. http://localhost:15672, 找到下面这个Exchange:github

clipboard.png

点击进入 http://localhost:15672/#/exch...async

而后就能够这样发消息了spa

clipboard.png

Demo

顺带发一个能跑的DEMO: https://github.com/developerw...code

说明

这里是修改的版本: https://github.com/developerw...graphql-rabbitmq-subscriptions 作了修改, 使其支持以下接口:server

asyncIterator<T>(triggers: string | string[]): AsyncIterator<T>;

RabbitMQ 默认安装, 不作任何修改. Ubuntu 下的安装命令为 apt-get install rabbitmq-server接口

Github Issue:
https://github.com/cdmbase/gr...rabbitmq

变动:

2017-09-09:
1.https://github.com/developerw...
使用 rabbitmq-pub-subRabbitMqSingletonConnectionFactory替换RabbitMqConnectionFactory,不然不会重用链接, 而且建立生大量的Queue
clipboard.pngip

2017-09-10:
如今graphql-rabbitmq-subscriptions包的兼容性已经修复, 直接yarn add graphql-rabbitmq-subscriptions便可.get

Pull Request: https://github.com/cdmbase/gr...

相关文章
相关标签/搜索