API Gateway Websocket

The basics and a quick tutorial

What is WebSocket routing in Amazon API Gateway?

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. With API Gateway, you can create REST and WebSocket APIs that enable real-time two-way communication between your back-end services and your API’s consumers.

WebSocket routing in Amazon API Gateway is a feature that allows you to create APIs that use the WebSocket protocol. With WebSocket routing, you can create APIs that enable real-time updates, notifications, and other interactive features in your applications.

In API Gateway, you can create WebSocket APIs that route incoming WebSocket messages from clients to specified back-end services, such as Lambda functions or other AWS resources. You can also specify the route that API Gateway should use to send messages from your back-end service to connected clients.

API Gateway provides a number of features to help you manage and secure your WebSocket APIs, including support for message filtering and transformation, access control and authorization, and custom error handling.

Read more about API gateways in a cloud native world

WebSocket API concepts

There are several key concepts that are important to understand when working with WebSocket APIs:

  • WebSocket connection: A persistent, full-duplex connection between a client and a server that allows both parties to send and receive data at any time. WebSocket connections are established using the WebSocket protocol and are typically initiated by a client, although the server can also initiate the connection.
  • WebSocket message: A piece of data that is sent over a WebSocket connection. WebSocket messages can be text or binary data and can be sent by either the client or the server.
  • WebSocket route: A path that defines how incoming WebSocket messages from clients should be routed to back-end services, such as Lambda functions or other AWS resources. WebSocket routes are defined in the API Gateway configuration for a WebSocket API.
  • WebSocket payload: The data that is contained in a WebSocket message. Payloads can be text or binary data, and they can be used to carry a variety of different types of information, such as commands, requests, responses, and data updates.
  • WebSocket client: A piece of software that connects to a WebSocket API and can send and receive messages over a WebSocket connection. WebSocket clients can be web browsers, mobile apps, or any other type of software that supports the WebSocket protocol.

Quick tutorial: Building a serverless chat app with a WebSocket API, Lambda, and DynamoDB

Here is a tutorial on how to build a serverless chat application using a WebSocket API with Lambda and DynamoDB.

Step 1: Create a WebSocket API

To create a new WebSocket API in Amazon API Gateway:

  1. Sign in to the AWS Management Console and go to the Amazon API Gateway dashboard.
  2. Click on the Create API button.
  3. Select WebSocket as the protocol and give your API a name and description.
  4. Click on the Create button to create your new WebSocket API.

Step 2: Define your WebSocket routes

To define the WebSocket routes:

  1. In the API Gateway dashboard, click on the WebSocket Routes tab.
  2. Click on the Create Route button.
  3. Give your route a name and specify the Lambda function or other back-end service that you want to invoke when this route is matched.
  4. You can also specify a route key pattern that will be used to match incoming WebSocket messages to this route. For example, you could use a route key pattern of sendMessage to match messages that contain the string sendMessage in the payload.
  5. Click on the Create button to create your new route.
  6. Repeat this process to create additional routes as needed.

Step 3: Create your Lambda functions

This step involves the following:

  1. In the AWS Management Console, go to the Amazon Lambda dashboard.
  2. Click on the Create function button.
  3. Give your function a name and select WebSocket as the trigger.
  4. Select the WebSocket API that you created in step 1 as the trigger for your function.
  5. Choose a runtime for your function, such as Node.js or Python.
  6. In the Function code section, write the code for your function using the Lambda runtime of your choice.
  7. Click on the Create function button to create your Lambda function.
  8. Repeat this process to create additional Lambda functions as needed.

Step 4: Create a DynamoDB Table

To create a DynamoDB table to store chat messages:

  1. In the AWS Management Console, go to the Amazon DynamoDB dashboard.
  2. Click on the Create table button.
  3. Give your table a name and specify a primary key. You could use a primary key such as timestamp or messageId to uniquely identify each chat message.
  4. Click on the Create button to create your DynamoDB table.

Step 5: Modify the Lambda functions

To modify your Lambda functions to read and write to the DynamoDB table:

  1. In the Amazon Lambda dashboard, edit one of your Lambda functions.
  2. In the Function code section, update your function to read and write to the DynamoDB table that you created in step 4. You will need to use the AWS SDK for your chosen runtime to interact with DynamoDB.
  3. Save your changes to the function.

Step 6: Test the app

To test your chat application:

  1. In the Amazon API Gateway dashboard, click on the Test tab.
  2. Connect to your WebSocket API using a WebSocket client, such as a web browser or mobile app.
  3. Send and receive messages through the chat application to test that it is working correctly.

API Gateway WebSocket best practices

There are several best practices for using Amazon API Gateway WebSocket APIs:

  • Use the WebSocket APIs for scenarios that require real-time communication, such as chat applications, gaming, and IoT.
  • Use built-in message filtering and routing features to route incoming messages to the appropriate backend service or Lambda function.
  • Use the AWS Lambda function integration to run serverless code in response to WebSocket events. This allows you to scale your WebSocket APIs without managing any infrastructure.
  • Use the Amazon Cognito integration to authenticate and authorize WebSocket connections. This allows you to control access to your WebSocket APIs and protect them from unauthorized access.
  • Use the Amazon CloudWatch integration to monitor and log WebSocket events and metrics. This allows you to track the performance and usage of your WebSocket APIs and troubleshoot any issues.
  • Use the Amazon API Gateway caching feature to improve the performance of your WebSocket APIs. This can help reduce the latency and improve the responsiveness of your WebSocket connections.
  • Use the custom domain feature to map your WebSocket APIs to a custom domain, such as wss://my-api.example.com. This can make your WebSocket APIs more user-friendly and easier to use.

Using Solo Gloo Gateway as an AWS API Gateway alternative

For many companies, Solo Gloo Gateway is a preferred API Gateway alternative to AWS API-GW, due to its performance, scalability, multi-cloud functionality, Kubernetes-native integration, and overall cost savings (often up to 40%). In addition to robust API-GW security, authorization and authentication, Solo Gloo Gateway also integrates seamlessly with AWS Lambda to enable serverless functionality.

Get started with Gloo Gateway today!

Cloud connectivity done right