“Simple” Serverless Websocket Design in AWS with REST API

I really liked how we designed our websocket architecture. After all the infrastructure is written, it’s easy to use and fast to implement new features.

Why Websockets?

Typical usage

  • Long process — a process takes a long time and we want update a user when the process is finally done.
  • Third party action — For example, Stripe confirms payment via webhook on the backend. The backend can then update the user of the successful payment.
  • Any communication with an active user.

Serverless Problem

Solution

Integrating Websockets with REST API

Example of the long running process flow

Step 2 — Register/connect with the Websocket Gateway. Our connect/disconnect lambdas save user mapping information on a database.

Step 3 — User makes the call to start the long running process.

Step 4 — Initial fast process lambda function notifies the long running lambda to start through AWS SNS (more hand waving, but again, plenty of examples on the web on how this is done).

Step 5.1/Step 6 — Immediately return to the user a status.

Step 7 — Run Long Running Process.

Step 8 — Long Running Process is done. Retrieve connection information from the database.

Step 9 — Publish information to client

Step 10 — AWS Websockets sends information back to the browser via websockets.

Step 11 — Browser retrieves data for changed event (see below).

Key Architecture Design that keeps this “Simple”

  1. The message we send to the client is just an event key like ‘chat-messages-updated’. There is no other parameters. The client or the React components interested in the ‘event’ will query the backend for updated information. For example, the nav bar component can update its ‘new message count icon’. This involves having a websocket context, that components can “listen” for changes. )Let me know if anybody is interested, I can write another article on this.)
  2. The method to send a message to a client takes only a user id and an event key.

Final Thoughts

Software Doodler with 20+ years of Noodling Experience

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store