我们使用 GraphQL Subscriptions 和 pubsub 来订阅帖子。
当发生超过 10 个订阅时,我们会收到节点警告“MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏”。
是否可以提高 pubsub 类中的最大侦听器?
pubsub 类位于一个单独的模块中,如下所示:
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,
};