Title: Intro to AWS WebSockets Part Four: User Notifications Publication: Ready, Set, Cloud! Author: Allen Helton Published: February 08, 2022 URL: https://www.readysetcloud.io/blog/allen.helton/intro-to-aws-websockets-part-four/ Sending notifications when long running tasks complete is key to success with async software. We can add that into our workflows in minutes with WebSockets. When you login to a website like Facebook, where do your eyes go first? The top of your feed? The stories banner? **Probably not.** Your eyes go straight to the toolbar to see if there is a little red badge showing you notifications or new friend requests. We can't help it, we love notifications. Every little notification we see fires a signal in our brains that gives us a sense of satisfaction. Honestly, we're addicted to notifications. So what better than to implement that with our WebSocket? If you've been following along, this is part four in an introductory series to WebSockets. Each part builds on the one before, so if you're joining us now, I'd highly recommend reading the first three parts. * [Part One - Building a WebSocket](/blog/allen.helton/intro-to-aws-websockets) * [Part Two - Securing your WebSocket](/blog/allen.helton/intro-to-aws-websockets-part-two) * [Part Three - Documentation Using Async API Spec](/blog/allen.helton/intro-to-aws-websockets-part-three) Today we're going to expand on what we've built to create **user notifications**. ## Deploy To The Cloud Just like the three previous parts, the work is done ahead of time. To deploy the updates that add user notifications, run the following command in a terminal in the root of [the repo](https://github.com/allenheltondev/serverless-websockets/tree/part-four) on your local machine. ``` git fetch git checkout part-four npm run deploy ``` This will checkout this portion of the walkthrough and deploy it to AWS. The updates included to support user notifications are: * A lambda function to search for and post to specific user connections * [Dead Letter Queues (DLQs)](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html) for event delivery/processing failures * [CloudWatch alarm](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html) for notifying when a failure has occurred * Updates to our Async API Spec that define new input and output events Fortunately, adding user notifications to what we already had was easy. Our data structure allowed for a *give me connections for a specific user* [access pattern](https://docs.aws.amazon.com/prescriptive-guidance/latest/dynamodb-data-modeling/step3.html), so the changes were simple. ## Find The User Connection With the WebSocket we built [in a previous post](/blog/allen.helton/intro-to-aws-websockets-part-two), we added a lambda authorizer to the *$connect* endpoint that saved the `userId` of the connecting user to the database. We can use this information to find the open connections for a user in order to send them push notifications. When a new `Send User Push Notification` EventBridge event comes in, we can query the database for all connections for that `userId` and send them a message. ![Using the connection details to receive a push notification](https://readysetcloud.s3.amazonaws.com/websocket_notifications_1.png) *Using the connection details to receive a push notification* In our data model, we save the user id as the `pk` in our `GSI` so we are able to run a query just on the user id that was provided in the incoming event. ``` { "pk": "", "sk": "connection#", "GSI1PK": "", "GSI1SK": "user#", "ipAddress": "", "connectedAt": "", "ttl": "