Real-time Messaging System with AWS API Gateway and Lambda

Intro

Location

London

Industry

EdTech

Team

1 front-end and 1 back-end engineer

Timeline

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.

Challenge

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.

Solution

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

Lambda

Amazon ElastiCache

ElastiCache

NAT Gateway

NAT Gateway

Amazon VPC logo

VPC

IAM logo

IAM

CloudWatch logo

Cloud Watch

Portfolio

ZAXID.NET News Portal Development Case Study

ZAXID.NET Online News Portal – high load web application architecture design and development by Romexsoft.

TIM Media

TIM is an ad serving platform, offering advertisers and publishers a variety of robust tools to maximize revenues, increase ad ROI and eliminate budget waste – powered by innovative tech solutions and granular data analytics. TIM is a trusted partner of Fortune 500 companies including Nissan, McDonalds, AT&T, Danone, Disqus and other well-known tech and product brands.

SavvyMoney

SavvyMoney is an online tool that provides users with a free access to their credit score, explains factors impacting it and gives suggestions how it can be improved. The aim of the company is to empower the control of financial health with easy understandable advice about credits and debts, help monitor the credit score according to the interest rates and provide users with more economic options.