Real-time Messaging System with AWS API Gateway and Lambda







1 front-end and 1 back-end engineer


1 month

Executive Summary

LearnCube is an online education platform that enables anyone, anywhere to access world-class teachers. It is designed as a virtual class room with live chat and other real-time collaborations like a whiteboard. Platform is trusted by top language schools, tutoring companies & corporate trainers like Oxford University Press, Lingoda and many others.


Having experienced significant growth due to COVID-caused shift to online education, the company faced the need to optimize costs and enhance customers experience. A significant share of expenses were paid to the commercial third party solutions, including a real-time messaging and collaboration solution based on WebSocket.


Main client’s infrastructure is running in AWS Cloud. Having positive experience in collaboration with Romexsoft before LearCube asked us to come up with a solution that meets two major goals: less expenses compared to existing solution and high reliability. To resolve this issue our company suggested to implement real time messaging system based on API Gateway with WebSockets, Lambda and ElastiCache services.
Amazon API Gateway is a fully managed service that handles APIs at any scale. Also it supports two-way  WebSocket APIs. Such events like WebSocket connect and disconnect are provided out of the box. So, the solution works in a following way:

  • WebSocket connection is initiated from the client side via vue.js and passed through API Gateway to the written in node.js AWS Lambda function that stores connection information like connectionId, userId and classRoomId in the AWS ElastiCache. API Gateway securely keeps the WebSocket connection in its pool
  • Client sends a message through the opened WebSocket connection to API Gateway’s onMessage endpoint. Another AWS Lambda handler gets classRoomId from the payload, gets all stored in ElastiCache connections with this classRoomId and performs broadcast of message body using AWS.ApiGatewayManagementApi SDK
  • When the client exits the classroom or class is finished the disconnect event is sent to API Gatewaydisconnect endpoint. AWS Lambda handler gets message info and removes it from ElastiCache. API Gateway removes connection from its pool

Custom AWS Lambda authorizer is used to initiate WebSocket connections from authenticated users only. AWS ElastiCache has primary and read replicas in different availability zones to support high availability and performance. Also ElastiCache is located in the VPC in a private subnet. So Lambda handlers are also placed in the VPC and NAT Gateway is set up to provide an external communication for AWS.ApiGatewayManagementApi SDK to broadcast messages. All Lambdas and API Gateway metrics are recorded in AWS CloudWatch logs.

Architectural Diagram

Real Time Messaging System AWS case study

Results and Benefits

  1. Decreased monthly costs for real-time messaging solution almost 3x times which was sizable monetary value.
  2. High reliability and guarantee in messaging delivery to the end client.
  3. Solution transparency and observability via CloudWatch logs and dashboards.

Utilized AWS services

Amazon API Gateway

API Gateway

Lambda logo


Amazon ElastiCache


NAT Gateway

NAT Gateway

Amazon VPC logo


IAM logo


CloudWatch logo

Cloud Watch