Serverless WebSockets on AWS
Tags: serverless, websocket, aws, appsync
Published: 2021-10-17 09:07:57

---
title: Serverless WebSockets on AWS
published: true
description: With AWS, developers have multiple choices on how to implement real-time communication. Let us go through some of the options.
tags: serverless, websocket, aws, appsync
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbp5uo2lfqk7zdgqi1bf.jpeg
---

Real-time communication between services or people is tricky to implement.

Thankfully, in the day and age of cloud computing, we have multiple options that one might use to implement reliable _WebSocket-based_ communication.

In this blog post, I will be talking about _WebSockets_ in the context of AWS. What are the options? What are their pros and cons? What kind of architectures could I deploy with each?

Let us find out.

## Amazon API Gateway WebSockets

[In the December of 2018, AWS announced a new capability of Amazon API Gateway](https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/) – _WebSockets_ support.

I remember being pretty excited about the addition. Since my go-to for learning a new programming language is a chat application (I find the _TODO style_ applications a bit boring), I immediately jumped into the service documentation to learn more.

![Basic API Gateway WebSockets architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhg8hybzu1ahurtje6zc.jpeg)

### Cons

My enthusiasm quickly faded after reading the documentation.

- Very **sub-optimal support for broadcast behavior**. To my best knowledge, there is **no way to natively send a message to multiple connections with a single API call**. The lack of native support for _topics_ or _rooms_ makes _Amazon API Gateway WebSockets_ a lesser choice when a broadcast functionality is required.

- The **state of individual connections needs to be somehow tracked**. Usually, in my experience, developers tend to use [_Amazon DynamoDB_](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) to do so. If you are on the lookout for a completely managed solution, you might be a bit disappointed.

### Pros

It is not all bad, though.

- Since you most likely already take advantage of _Amazon API Gateway_, the complexity of adding _WebSockets_ to your architecture is greatly reduced.

- With the _Amazon API Gateway_ direct integrations capabilities, one might create elaborate and resilient _WebSocket_ backend without needing _AWS Lambda_ functions.
  I took on that challenge myself. [Here is the result](https://github.com/WojciechMatuszewski/cdk-lambdaless-apigw-websockets).

- The capability has relatively few moving parts and, **in my opinion**, is very approachable and a blast to learn.

### Architectures

I would argue that the _Amazon API Gateway WebSockets_ is an **excellent alternative to pooling**. Depending on the traffic your service gets, it might be cheaper to maintain a _WebSocket_ connection rather than making periodic requests to the API. [Here is an excellent resource on this subject](https://aws.amazon.com/blogs/compute/from-poll-to-push-transform-apis-using-amazon-api-gateway-rest-apis-and-websockets/).

## AWS AppSync GraphQL subscriptions

[_AWS AppSync_](https://aws.amazon.com/appsync/) is a service that enables you to create managed _GraphQL_ APIs. The service is rapidly gaining popularity as _GraphQL_ enters the engineering mainstream.

The real-time functionality is [exposed through _GraphQL subscriptions_](https://docs.aws.amazon.com/appsync/latest/devguide/aws-appsync-real-time-data.html). Since explaining what _GraphQL subscriptions_ is out of scope for this article, I encourage you to do some exploration on your own if you are not familiar with the term.

![AppSync WebSockets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgyi2cdali66nqn017l0.png)

### Cons

- In my humble opinion, there are **a lot of gotchas** that you might encounter on your implementation journey. [Here is a great article that touches on this subject](https://blog.purple-technology.com/lessons-learned-aws-appsync-subscriptions/).

- On the frontend side of things, you **might encounter issues with integrating the _AWS AppSync_ JavaScript client with one of your _GraphQL_ client libraries**. As of writing this, [there is a lot of issues on GitHub](https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues?q=apollo) related to integration with my favorite GraphQL client – the [_apollo-client_](https://github.com/apollographql/apollo-client).

- This might be obvious at this point, but I still want to bring it up. **To my best knowledge**, the **functionality is only available in the context of _GraphQL_**. It would be very nice to have a _non-GraphQL_ specific version of _AWS AppSync subscriptions_. On the other hand, maybe such a service already exists? Read on to find out!

### Pros

- Holy moly, you can get a lot done with _AWS AppSync_. **If things work, the service will save you a lot of time**. In my humble opinion, the basics are easy to grasp, and if you are familiar with _GraphQL_ already, you should have an easy time grasping how _AppSync subscriptions_ work.

- The _AWS AppSync_ subscriptions **scale very well**. [According to AWS](https://aws.amazon.com/about-aws/whats-new/2020/04/aws-appsync-enables-support-for-generic-websockets-clients-graphql-real-time-subscriptions/) you should have no problems **scaling to millions of connections**. Speaking from my experience, I was able to push the service to about 500k connections without any issues.

### Architectures

There are so many choices when it comes to _AWS AppSync_. The service lends itself very well to almost, if not all, situations where _GraphQL subscriptions_ are a requirement.

My advice would be to get acquainted with [_AWS AppSync_ use cases section](https://aws.amazon.com/appsync/) of the service page.

## AWS IoT Core MQTT

When I first heard about the _AWS IoT_ suite of services, the last thing that came to my mind was rich and extensible support for _WebSockets_. Learning more, I was pleasantly surprised to discover that the service exposes completely managed _MQTT_ topics.

![IoT Core MQTT rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35t78ry8p43a3bxq6nj7.jpeg)

### Cons

- To understand the _WebSockets_ capabilities fully, **one needs to get familiar with many service-specific moving parts**. What is a _device shadow_? Do I need some kind of certificate for authentication purposes? What is _data endpoint_? These are some of the questions I asked myself while trying to figure out how the service works.

- It might be that I live in my own "community bubble", but **I have not seen a lot of teaching materials regarding utilizing _WebSockets_ using _AWS IoT Core_**. You might find it challenging to find answers to your questions. [Here is the blog post I was referring to while playing with the service](https://medium.com/serverlessguru/serverless-real-time-reactjs-app-aws-iot-mqtt-17d023954045).

### Pros

- _WebSockets_ via _AWS IoT Core MQTT_ are **independent of API architecture**. Are you rocking your own _GraphQL_ server? or maybe you prefer REST APIs? In either case, it is possible to integrate with _IoT Core MQTT_. Remember me alluding to _non-GraphQL_ specific service for handling _WebSockets_? This is the service I was referring to.

### Architectures

Anything related to IoT is not my area of expertise. That did not stop me with experiments, though.

I believe that _AWS IoT Core MQTT_ could be used **for real-time frontend feature toggles**. Integrating your frontend with the service should be hassle-free because of the [_AWS Amplify_ plugin](https://docs.amplify.aws/lib/pubsub/getting-started/q/platform/js/). [Here is my attempt at building real-time frontend feature toggles with _AWS IoT Core_](https://github.com/WojciechMatuszewski/iot-core-feature-flags).

## Closing words

Developers have many options when it comes to serverless _WebSockets_ on AWS – from _GraphQL_ specific service to arguably surprising usage of _AWS IoT Core MQTT_ topics.

I hope that by reading this article, you have learned something new. I intended to provide basic information about each service and encourage you to explore further on your own.

If you have any questions, feel free to drop me a DM on Twitter - [@wm_matuszewski](https://twitter.com/wm_matuszewski).

Thank you for your time.