vaughan
vaughan

Reputation: 7472

Why do GraphQL Subscriptions use an AsyncIterator?

AsyncIterator requires pulling data using .next(). But with websockets I generally want to push data when events occur. Only thing I can think of is that by using pull-based they can rate-limit.

So what is calling .next()? Is it a timer, or does it listen to a publish message, queue that, then call .next() until it consumes all the queue?

Is this suitable for real-time data, like GPS positions on a map?

Looked here and still could not figure it out: https://github.com/facebook/graphql/blob/master/rfcs/Subscriptions.md

GraphQL Subscriptions repo from Apollo: https://github.com/apollographql/graphql-subscriptions

Upvotes: 0

Views: 1539

Answers (1)

Tobino
Tobino

Reputation: 942

AsyncIterator iterate through an EventStream, then each event is resolved, sometimes with filter and/or with a payload manipulation.

Payload manipulation can call an another async database request, or resolve other GraphQL Types, which is time consuming.

So GraphQL use a pull-based system to rate-limit resolves eventStream. If you don't use withFilter neither resolves, you won't have delay on Event except with a lot of user.

GraphQL is suitable for low latency data.

Source: https://github.com/graphql/graphql-js/blob/master/src/subscription/subscribe.js#L44

Upvotes: 1

Related Questions