You are viewing the preview version of this book
Click here for the full version.

Real-time data with subscriptions

AppSync has support for a third operation besides Queries and Mutations: Subscriptions. Subscriptions are a way to push data from the API to the clients in a way that fits the GraphQL world.

The nicest thing about subscriptions is that it is fully managed by AppSync. You don't need to keep track of clients and implement a fanout to a potentially large number of targets as all this is managed by AWS. All you have to do is define who can listen to what and then trigger the messages. While this is no small task, as we'll see in the next chapters, this is essentially a developer's work instead of an architect's.

When a client uses a subscription, it sends GraphQL queries for various topics then AppSync sends a notification message when an event happens. Under the hood, it is a WebSocket channel with a protocol that handles multiple independent subscriptions.

Subscriptions use a WebSocket channel to push data to the clients

With a real-time channel like this, clients don't need to poll the API for new data as the backend can push any changes to them. This enables client applications to show always up-to-date data without reloading the page.

In this chapter, we'll see how to implement pushing data to clients via subscriptions and discuss the various pitfalls and best practices.

Pushing data to clients

But first, let's talk about real-time notifications in general! While it sounds simple to push something to the clients and that magically transforms the user experience to the 21st century, but this is a nuanced topic.

There is more, but you've reached the end of this preview
Get a subscription to get access to the full book.