Serverless Blog
all
guides & tutorials
user stories
operations & observability
engineering culture
news

Real-time applications with API Gateway WebSockets and AWS Lambda

written by  
  Jared Short

AWS just announced the launch of a widely-requested feature: WebSockets for Amazon API Gateway. This means Framework users around the world finally have a straightforward way to create client-driven, real-time applications via WebSockets.

Read on for more info on how this changes the game for real-time development, and the Serverless Framework plans to support WebSockets in API Gateway!

Life before WebSockets support

Sure, there were hack-y ways to do real-time applications before.

You could, for instance, use AWS IoT topics with MQTT over WebSockets, and I have even heard of folks running containers or cluster to broker WebSocket connections even though the rest of their systems are serverless.

There is also fanstastic WebSocket support in AWS AppSync, but this introduces some complexities with a GraphQL layer and you give up a bit of control in exchange for simplicity. Undoubtedly a great solution if it can meet your needs.

WebSockets support makes real-time so much easier

The WebSockets support that was announced today means developers have much more control over the WebSocket layer itself, delivering payloads directly to lambda functions and shuttling results back.

There are other classes of services out there, like Ably or PubNub, which offer solutions in this space. But staying closely integrated to your platform provider for core services can make a lot of sense for many organizations. Security teams and billing departments appreciate working with a known vendor.

A real-time WebSockets example

To understand the power of this new feature, let’s look for example architecture building the canonical “chat” example for real-time WebSocket driven applications.

WebSockets chat app

With these native WebSockets in API Gateway, you establish a single WebSocket connection to API Gateway from the device. AWS Lambda is notified of the connection in your normal event-driven compute method. You get some metadata, the payload and a connectionId that you use later.

It's probably a good idea for you to store this connectionId and information the device sent you (perhaps topics or channels they are subscribed to) in a datastore such as DynamoDB, so you can reference it later when needed.

Next, say someone wants to send a new message out to the channel. They would send it over their established WebSocket to API Gateway, to a waiting AWS Lambda function. On invoke, the business logic would check your datastore for the connections subscribed to that channel, and callback to API Gateway with the connectionId and your payload.

API Gateway will take it from there and send your payload through on the established WebSocket connection.

You Lambda function would be invoked on disconnects as well, allowing you to clean things up in your data store so you don't waste cycles trying to send messages to non-existent connections.

In sum: this is simple, event-driven, and real time. This single feature makes a whole new class of applications first class citizens in the serverless ecosystem!

Serverless Framework support

WebSockets for API Gateway is unfortunately not GA yet.

We are working to bring this new feature to the Serverless Framework ASAP so you can leverage it as it becomes available, so stay tuned!

About Jared Short

Jared Short is the Head of Developer Relations and Experience at Serverless, Inc.

guides-and-tutorialsoperations-and-observability - 25.09.17

How to deploy multiple micro-services under one API domain with Serverless

Learn how to use the same domain name for multiple Serverless services with API Gateway base path mappings.

written by Alex DeBrie

guides-and-tutorials - 04.12.18

Building an API with Ruby and the Serverless Framework

AWS Lambda now supports Ruby! Here's how you can get started and build an API with the Serverless Framework.

written by Jared Short

Join 16,000 other developers and keep up to speed on the latest serverless news and releases.

Join 16,000 other developers and keep up to speed on the latest serverless news and releases.

Made with love in San Francisco + Atlanta, Austria, Germany, Pakistan, Poland, Nebraska & Thailand

Serverless, Inc. © 2018

Join our monthly newsletter to get the latest Serverless news, updates and happenings.