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

Event filtering

Try it yourself

You can find code example for this chapter here.

So far we've covered only how to trigger and consume subscription events. Now we'll look into it how to filter the events. Filtering is essential for two reasons.

First, sending all notifications to all clients does not scale. As the system grows, clients should receive only the events that they are interested in and shouldn't be bothered with everything else.

And second, security. Since notifications contain data, they should be access controlled just like normal queries. Imagine everybody getting all the Facebook messages somebody sends. That would expose every single chat conducted on that platform. Notifications almost always contain sensitive data which means it's important to send them only who should have access to the data.

In the case of AppSync, imagine that all notification events go into a big bucket and filtering determines what to send to whom. This is a bit different than traditional real-time updates where you manage the connections as you can target individual clients. Here, there is no targeting but only filters, determined when clients subscribe.

AppSync sends the subscription event to clients based on filters

AppSync provides two ways to filter messages. The original, arguments-based filtering and a newer addition, called enhanced filtering. The latter is more powerful but also way more complicated and in my experience it is useful in only a couple of cases. This means, the majority of subscriptions will likely use arguments-based filtering, that's why we'll cover both ways.

Subscription filters

Subscription filters are determined when a client sends the subscription request. These is no way to change the filters for already subscribed clients.

Arguments-based filtering

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