GraphQL订阅:侦听器数超过警告 [英] GraphQL Subscriptions: Max Listeners Exceeded Warning

查看:129
本文介绍了GraphQL订阅:侦听器数超过警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用GraphQL订阅和pubsub来订阅帖子.

We are using GraphQL Subscriptions and pubsub to subscribe to posts.

当发生10个以上的订阅时,我们将收到节点警告"MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏".

When more than 10 subscriptions occur we get the the node warning "MaxListenersExceededWarning: Possible EventEmitter memory leak detected."

是否可以提高pubsub类中的最大侦听器?

Is it possible to raise the max listeners in the pubsub class?

pubsub类位于单独的模块中,如下所示:

The pubsub class is inside a separate module and looks like this:

import { PubSub } from 'graphql-subscriptions';

const pubsub = new PubSub();

export { pubsub };

订阅服务器如下所示:

import { SubscriptionManager } from 'graphql-subscriptions';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';

import { pubsub } from './subscriptions';
import executableSchema from './executableSchema';

const WS_PORT = 8080;

const websocketServer = createServer((request, response) => {
  response.writeHead(404);
  response.end();
});

websocketServer.listen(WS_PORT, () => console.log(
  `Websocket Server is now running on http://localhost:${WS_PORT}`
));

const subscriptionManager = new SubscriptionManager({
  schema: executableSchema,
  pubsub: pubsub,
  setupFunctions: {
        newPost: (options, args) => {
         return {
            newPostChannel: {
               filter: (post) => {
                  return args.publicationId === post.relatedPublication.id;
               }
            },
         };
      },
  },
});

const subscriptionServer = new SubscriptionServer({
  subscriptionManager: subscriptionManager
}, {
  server: websocketServer,
  path: '/',
});


export {
  subscriptionServer,
};

推荐答案

我编写了您正在使用的graphql-subscriptions包的原始实现,因此我可以在此处提供一些上下文.

I wrote the original implementation of the graphql-subscriptions package you're using, so I can provide some context here.

graphql-subscriptions中包含的简单EventEmitter pubsub库仅用于演示目的. EventEmitter并不会真正大量扩展,它们在内存中,并且只有在您只有一台服务器的情况下,它们才可以工作.

The simple EventEmitter pubsub library included in graphql-subscriptions is only intended for demo purposes. EventEmitters don't really scale to large numbers, they're in-memory, and they'll only work as long as you have no more than a single server.

对于试图在生产环境中运行GraphQL订阅的任何人,我强烈建议使用其他系统,例如通过 graphql-redis-subscriptions graphql-mqtt-subscriptions .这样做的好处是可以使GraphQL服务器保持无状态(除了websocket之外),从而易于水平扩展.

For anyone trying to run GraphQL subscriptions in production, I strongly recommend using a different system, for example Redis or MQTT through graphql-redis-subscriptions or graphql-mqtt-subscriptions. This will have the advantage of keeping the GraphQL server stateless (apart from the websockets) and thus easy to scale horizontally.

这篇关于GraphQL订阅:侦听器数超过警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆