简体   繁体   English

带有多线程NodeJS服务器的Graphql订阅

[英]Graphql subscription with multithreaded nodeJS server

I am trying to create a graphql subscription service in a multi-threaded architecture. 我正在尝试在多线程体系结构中创建graphql订阅服务。

If I, for example, create a subscription like below 例如,如果我创建一个如下订阅

const SOMETHING_CHANGED_TOPIC = 'something_changed';

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),
    },
  },
}

and have multiple instances of identical nodeJS server running on my docker cluster, I do not want each of the instances to send subscription event to the client every time new subscription publishing has been made. 并且在我的nodeJS集群上运行了多个相同的nodeJS服务器的实例,我不希望每个实例在每次进行新的订阅发布后都将订阅事件发送给客户端。 I only want one (of course preferrably load balanced among the clusters) to be responsible for sending subscription to the client. 我只希望一个(当然最好在群集之间实现负载平衡)负责向客户端发送订阅。

Only way I can think of doing this right now is to create an isolated server that is in charge of subscription event and only receive subscription event from a single source as a client. 我现在想到的唯一方法是创建一个隔离的服务器,该服务器负责订阅事件,并且仅作为客户端从单个来源接收订阅事件。 But same multi-threading problem I have mentioned above occurs if I spread out the load of the subscription-responsible servers. 但是,如果我分散负责预订的服务器的负载,则会发生我上面提到的相同的多线程问题。

How do I make sure that I only send out single subscription event to the client when there are multi-threade nodeJS backend? 当有多线程nodeJS后端时,如何确保仅向客户端发送单个订阅事件? Is it only possible with the client side filtering? 只能使用客户端过滤吗? ie ignore events that have already arrived - However I think this is a terrible waste of resource as subscription grows. 即忽略已经发生的事件-但是,随着订阅的增加,这是对资源的严重浪费。

In this case I would use Redis as a PubSub, that way you have "one source of truth". 在这种情况下,我将Redis用作PubSub,这样您就拥有了“一个真实的来源”。 ie https://github.com/tomyitav/redis-messaging-manager . https://github.com/tomyitav/redis-messaging-manager

I'm sure there are plenty of other libraries that do something similar 我敢肯定还有很多其他类似的库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM