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

Implementing subscriptions

Code example

Example code is available here.

AppSync provides a real-time channel to push updates to clients in a way that fits into the GraphQL world: the client sends a subscription request that uses the same language as queries and mutations where it defines arguments for the field and what data it needs. As we've discussed earlier there are quite a few complications on the backend-side, but in this chapter we'll focus on why it's hard to implement subscriptions properly on the client.

AppSync's subscription protocol builds on top of WebSocket which means a single channel can transfer multiple subscriptions. This is a very efficient way to implement pushing, but it also brings complications in terms of connection management and multiplexing.

While AWS provides a surprisingly good documentation about what messages the client and the server sends, you probably don't want to start from there. Instead, there are client libraries that implement the low-level part of the protocol and then you only need to define what subscriptions to send and how to process the incoming data.

AWS provides the Amplify client library that manage the connection for you. This integrates with the other client-side parts of the AWS ecosystem, such as managing login with Cognito and automatically attaching the access token for the session.

What I don't like about Amplify is that it tends to permeate the whole application even if I only need a small part of it. It relies on a global configuration, includes a lot of services by default, and it's not easy to separate the frontend-only parts from the full-stack.

There is more, but you've reached the end of this preview
Read this and all other chapters in full and get lifetime access to:
  • all future updates
  • full web-based access
  • PDF and Epub versions