Intro to AWS Websockets
Tags: aws, api, tutorial
Published: 2022-01-19 15:15:57

---
title: Intro to AWS Websockets
published: true
description: Websockets are key to polished async processes. Discover how to start with push notifications in AWS 
tags: 
  - aws
  - api
  - tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5biy41m6f7771gqyihx4.jpg
canonical_url: https://www.readysetcloud.io/blog/allen.helton/intro-to-aws-websockets/
---

You know that feeling when you go to a social media site, you publish something exciting, and sit there waiting for notifications to roll in as people like and comment on your post?

It's the best. 

It gives you immediate satisfaction over and over again. It sends little shots of dopamine through your system. It's literally addicting.

And it's all possible because of *WebSockets*.

### What is a WebSocket?

At its simplest, a WebSocket is a direct line of communication between a client and server. Let's use Twitter as our social media use case. A WebSocket connection is a two-way communication between the mobile app (or website) and the Twitter servers. 

When somebody likes your post or retweets you, you want to be informed right then and there. So the servers use the direct line it has with you to push a notification to your phone. You get to know what happened as soon as it is happening.

Are they necessary? Technically no. But do they enhance the user experience and bring delight to your consumers? Absolutely.

### Starting With AWS WebSockets

In AWS, a WebSocket is an [API Gateway v2 resource](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html). It supports the two-way communication between the client and server and allows you to setup custom routes.

Today we're going to go through an [example of a WebSocket](https://github.com/allenheltondev/serverless-websockets/tree/part-one) written in a SAM template. I chose a [SAM template](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification.html) because of how easy it is to deploy a serverless application directly into AWS without the need for 3rd party services.

To get started, navigate to the [example repo](https://github.com/allenheltondev/serverless-websockets/tree/part-one) and follow the deployment commands. If you have the AWS CLI and SAM CLI already installed on your machine, you should be up and running in less than 5 minutes!

### WebSocket Routes

Like any API, a WebSocket has routes that trigger functionality. At a minimum, a WebSocket API must have two routes:

* *$connect* - What happens when a new direct line/connection is made from the client to the server
* *$disconnect* - What happens when a direct line/connection is removed between the client and the server

In our example, we have lambda functions that are executed at each one of these endpoints. We persist important connection information like the `connectionId`, `ipAddress`, and `connectedAt` time to have a little bit of metadata around who is establishing connections. This follows the basic [Api Gateway -> Lambda -> DynamoDb](https://serverlessland.com/patterns/apigw-lambda-dynamodb) pattern, which is a foundational building block in all of serverless development.

There are two additional routes in the example WebSocket API we just deployed. 

* *subscribe* - Subscribes to receive push notifications when a particular entity is updated
* *unsubscribe* - Unsubscribes from push notifications for a particular entity

Most WebSocket examples found readily available on the internet are for [*broadcast* notifications](https://www.freecodecamp.org/news/real-time-applications-using-websockets-with-aws-api-gateway-and-lambda-a5bb493e9452/). These send notifications to every current connection. In a more real-world use case, a user would want to get updates when their user or an entity they are working on has been modified.

So our example provides a way to slim down WebSocket push notifications to specific entities on a connection by connection basis. 

### Testing the Connection

In order to test your WebSocket, you can grab the `WebsocketUri` output from your SAM deployment and connect to it in [Postman](https://www.postman.com). Postman is an application designed for everything around APIs, including connecting to WebSockets. 

If you do [serverless development](https://www.readysetcloud.io/blog/allen.helton/decoding-the-aws-serverless-design-principles) and are not actively using a tool like Postman, I highly recommend it. I have written [several blog posts](https://www.readysetcloud.io/tags/postman) about how it can improve developer experience and [strengthen your deployment pipelines](https://www.readysetcloud.io/blog/allen.helton/dynamic-test-generation-with-oas).

To connect to your new WebSocket in Postman, perform the following steps:

1. Open the [desktop app](https://www.postman.com/downloads/)
2. Select New -> WebSocket Request
3. Enter the route from the output of your SAM deployment in the address field
4. Click the `Headers` tab and add the `Sec-WebSocket-Protocol` header with the value `websocket`
5. Hit **Connect**

![Connected to our WebSocket in Postman](https://readysetcloud.s3.amazonaws.com/websocket_intro_1.png)
*Connected to our WebSocket in Postman*

When we want to subscribe or unsubscribe to updates, we must add a payload describing which route to take. With the API we built, the `action` property describes the route to take. To add a subscription, send a message in this format:

```json
{
  "action": "subscribe",
  "entityId": "myTestEntityId"
}
```

Again, the `action` property is telling the API which route to take, and the `entityId` property is a parameter we're passing into the *subscribe* lambda function deployed in the example project.

Now that we're connected to the WebSocket, add this message to Postman and hit the *Send* button. It will hit the endpoint and save the subscription.

![Adding a subscription for entity updates](https://readysetcloud.s3.amazonaws.com/websocket_intro_2.png)
*Adding a subscription for entity updates*

With the connection and subscription, we're ready to test our push notification!

### Sending Push Notifications

The first thing we want to do is take a look at [DynamoDB](https://aws.amazon.com/dynamodb) and verify our connection and subscription were saved. If you navigate to the [websocket table in the DynamoDB console](https://console.aws.amazon.com/dynamodbv2/home#item-explorer?initialTagKey=&table=websocket) you will see there are two entries present: a *connection* entry and a *subscription* entry.

![WebSocket connection and subscription in DynamoDB](https://readysetcloud.s3.amazonaws.com/websocket_intro_3.png)
*WebSocket connection and subscription in DynamoDB*

Now that we've confirmed our connection and subscription, we can hop over to the [EventBridge console](https://console.aws.amazon.com/events/home?#/eventbuses) to manually add a message.

In the example project we pushed into AWS, push notifications are triggered by an EventBridge event. The event is added to an SQS queue, which is popped by a lambda. The lambda function looks for all subscriptions for the `entityId` provided in the event and sends a push notification to all subscribers.

To manually send the event, click the *Send Events* button in the EventBridge console and add the following details as described in this screenshot

![EventBridge event details](https://readysetcloud.s3.amazonaws.com/websocket_intro_4.png)
*EventBridge event details*

Once you hit the *Send* button, the notification process will trigger and a push will be sent to your connection in Postman.

If we flip back to Postman, we can see our *Hello world* message was received!

![The push notification worked!](https://readysetcloud.s3.amazonaws.com/websocket_intro_5.png)
*The push notification worked!*

In a real world scenario, all subscribers of entity `myTestEntityId` would have received the same message we did. Super cool!

### Next Steps

In today's walkthrough, we learned about the pieces that make up a WebSocket and got access to a quick start with subscriptions and notifications. 

Over the coming weeks, I will be creating posts in a WebSocket series describing ways to add authentication, custom domains, and defining events through an [Async API spec](https://www.asyncapi.com/).

Feel free to use this code however you like. It's not quite production ready, it needs error handling states and retry mechanisms, but it's almost there. It was intended to illustrate all the moving parts that come with an AWS WebSocket API. 

There are many things to explore with WebSockets, and I will be here to walk you through it as I continue my adventure down an [async 2022](https://www.readysetcloud.io/blog/allen.helton/the-2022-word-of-the-year-is).