• Stars
    star
    419
  • Rank 102,414 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A demo application showing how to deploy a scalable realtime chat application powered by Socket.io, Node.js, Docker, and AWS Fargate. Includes full text search powered by OpenSearch Serverless

Fargate.chat

app

A Slack-like chat app built with Node.js and Vue.js and deployed using Amazon Web Services.

This application uses Docker containers in AWS Fargate, and AWS App Runner, as well as AWS Lambda functions.

A sample version of the app is hosted at: https://fargate.chat

Architecture features

  • No EC2 instances. One of the goals of this application architecture is that it is very hands off, nothing to manage or update. Serverless features are turned on wherever possible.
  • Fully defined as infrastructure as code, using AWS CloudFormation to create all the application resources.
  • Included docker-compose.yml which stands up a local copy of DynamoDB, and local copy of the socket.io chat server. This allows you to develop locally and see the application at http://localhost:3000. The only feature that does not run locally, and is just simulated is the full text chat search.
  • Integration test suite container that you can run against a copy of the application, either locally or remotely.

Read more about the architecture and services in use.

Application features

  • User account functionality, including anonymous accounts
  • Real time chat message sending over WebSocket protocol, with persistance in DynamoDB
  • Live user presence, including announcments when users join or leave
  • Unread message indicator
  • Typing indicator, including support for multiple typers at once.
  • Full text search for chat messages, powered by Amazon OpenSearch Serverless
  • Infinite virtual DOM scrolling, for performant feeling even when many thousands of messages have been sent in a room.

Things to note in this app

  • Working configuration for routing WebSocket connections with Socket.io through CloudFront
  • Example of how to setup OpenSearch access policies for a Lambda function
  • Horizontally scalable Socket.io using the Redis adaptor and Amazon ElastiCache

Setup instructions

Install if not already installed:

# Setup some env variables for later
# Can try other regions as long as all required services are in that region
export AWS_REGION=us-east-2
export AWS_ACCOUNT=$(aws sts get-caller-identity --query Account --output text)

# Install the dependencies off of NPM
(cd services/message-indexer; npm install)
(cd services/message-search; npm install)

# Setup the base VPC and networking stuff.
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/cluster.yml \
  --stack-name chat-cluster \
  --capabilities CAPABILITY_IAM

# The shared resources like DynamoDB table and OpenSearch Serverless
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/resources.yml \
  --stack-name chat-resources \
  --capabilities CAPABILITY_IAM

# Create an ECR repository to host the container image
aws ecr create-repository \
  --region $AWS_REGION \
  --repository-name fargate-chat

# Login to the ECR repository
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com

# Build the Docker image
docker build -t fargate-chat ./services/socket

# Upload the built container image to the repository
docker tag fargate-chat:latest $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/fargate-chat:latest
docker push $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/fargate-chat:latest

# Start up the container that runs in AWS Fargate
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/chat-service.yml \
  --stack-name chat-service \
  --capabilities CAPABILITY_IAM \
  --parameter-overrides ImageUrl=$AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/fargate-chat:latest

# Deploy the component which indexes sent chat messages
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/message-indexer.yml \
  --stack-name chat-message-indexer \
  --resolve-s3 \
  --capabilities CAPABILITY_IAM

# Deploy the component which provides search autocomplete and API Gateway
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/message-search.yml \
  --stack-name chat-message-search \
  --resolve-s3 \
  --capabilities CAPABILITY_IAM

# Build the component which hosts static web content
aws ecr create-repository \
  --region $AWS_REGION \
  --repository-name apprunner-web

# Build the Docker image
docker build -t apprunner-web ./services/web

# Upload the built container image to the repository
docker tag apprunner-web:latest $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/apprunner-web:latest
docker push $AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/apprunner-web:latest

# Launch the web container inside of App Runner
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/web.yml \
  --stack-name chat-web \
  --resolve-s3 \
  --parameter-overrides ImageUrl=$AWS_ACCOUNT.dkr.ecr.$AWS_REGION.amazonaws.com/apprunner-web:latest \
  --capabilities CAPABILITY_IAM

# Deploy CloudFront distribution which ties main app and search endpoint together on one domain
sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/cloudfront.yml \
  --stack-name chat-cloudfront \
  --resolve-s3 \
  --capabilities CAPABILITY_IAM

# Get the application URL to view it
aws cloudformation describe-stacks \
  --stack-name chat-cloudfront \
  --query "Stacks[0].Outputs[?OutputKey==\`PublicURL\`].OutputValue" \
  --output text

# Open the URL in the browser window

Project layout

/deps - Folder used only for local development purposes
/infrastructure - CloudFormation templates used for deployment to AWS
/services - Independent code components that make up the app
  /socket - The core Node.js socket.io app, which runs in AWS Fargate
  /message-indexer - Lambda function which triggers on DynamoDB updates, to index chat messages
  /message-search - Lamdba function triggered by API Gateway, answers search queries
  /test-suite - Container used for local tests, runs integ tests against socket service
  /message-index-drop - Admin Lambda function which drops the OpenSearch Serverless collection
  /web - Frontend service which serves the static web files in production

Admin and dev actions

If you want to drop the search index (will be recreated next time you send a message, though older messages will no longer be remembered)

sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/message-index-drop.yml \
  --stack-name chat-index-drop \
  --resolve-s3 \
  --capabilities CAPABILITY_IAM

Run a local copy of the socket app for testing:

make run    # Standup local DynamoDB and local stack
make test   # Rebuild and run tests locally

# Open localhost:3000 in browser to view app

Launch the public facing CloudFormation stack with a custom domain name and SSL cert:

sam deploy \
  --region $AWS_REGION \
  --template-file infrastructure/cloudfront.yml \
  --stack-name chat-cloudfront \
  --resolve-s3 \
  --capabilities CAPABILITY_IAM \
  --parameter-overrides DomainName=fargate.chat CertArn=arn:aws:acm:us-east-1:228578805541:certificate/45b4037a-2257-48ff-b3ae-b8e3e0523e85

More Repositories

1

awesome-ecs

A curated list of awesome ECS guides, development tools, and resources
2,797
star
2

clui

Command Line UI toolkit for Node.js
JavaScript
1,663
star
3

aws-cloudformation-fargate

Sample CloudFormation templates for how to run Docker containers in AWS Fargate with various networking configurations
599
star
4

s3-upload-stream

A Node.js module for streaming data to Amazon S3 via the multipart upload API
JavaScript
344
star
5

nodejs-aws-workshop

Learn to deploy a Node.js API using Elastic Beanstalk, AWS Lambda, Elastic Container Service, Amazon Fargate, and Kubernetes
JavaScript
184
star
6

ecs-cloudformation

Simple, production ready CloudFormation templates for launching containers on Amazon ECS and AWS Fargate
183
star
7

exiftool

A Node.js wrapper around exiftool, providing metadata extraction from numerous audio, video, document, and binary filetypes
JavaScript
79
star
8

autohotkey-windows-10-apple-magic-keyboard

AutoHotKey script that allows you to use an Apple Magic Keyboard in Windows 10 with Apple familiar keyboard shortcuts.
AutoHotkey
45
star
9

aws-ecs-deployment-patterns

A visual guide to deployment patterns on Amazon EC2 Container Service
39
star
10

screenshot-service

JavaScript
24
star
11

greeter-app-mesh-cdk

An example of how to use AWS Cloud Development Kit to setup an AWS App Mesh service mesh in AWS Elastic Container Service
JavaScript
22
star
12

greeter-cdk

Example AWS Cloud Development Kit app that deploys the greeter microservice stack
JavaScript
19
star
13

emma-sdk

Node.js client for the Emma API
JavaScript
16
star
14

webkit-html-to-image-phantomjs

A simple Phantom.js webkit HTML to Image conversion service
JavaScript
8
star
15

json-template

A satirical programming language inspired by MongoDB queries.
JavaScript
7
star
16

standard-deviation-stream

A Node.js class for pulling stats from a stream.
JavaScript
6
star
17

ecs-fargate-benchmark-templates

A collection of CloudFormation templates that can be used to test various task launch scenarios of ECS on EC2 and ECS on Fargate
6
star
18

classic-interview-algorithms

Collection of algorithms that are asked as classic interview questions
JavaScript
5
star
19

inlets-on-ecs-anywhere

An AWS Cloud Development Kit architecture for deploying Inlets as an ingress for an ECS Anywhere cluster
TypeScript
5
star
20

aws-cdk-nyan-cat

CSS
4
star
21

deploying-container-to-fargate-using-aws-copilot

Deploying a container to AWS Fargate using AWS Copilot
JavaScript
4
star
22

archer-nyan-cat

CSS
4
star
23

fargate-security-con414

4
star
24

nathanpeck

3
star
25

aws-cdk-advanced-ecs-scheduling

JavaScript
3
star
26

string-reverse

A sample application that just reverses a string, for an AWS Copilot demo
JavaScript
2
star
27

ecs-patterns-vitepress

CSS
1
star
28

Dumatenseb

Version controlled dwarf fortress world
1
star
29

wordladder

A JavaScript code interview snippet
JavaScript
1
star
30

euphoria

A bunch of code I wrote in 2004 in the EUPHORIA language
Eiffel
1
star
31

sumtime

Simple Nodes.js service for high volume data point sums and ranges
JavaScript
1
star
32

cdk-and-copilot

TypeScript
1
star
33

aws-cdk-github-actions

Test repo for experimenting with AWS CDK + Github Actions
TypeScript
1
star
34

liquid-clock

Liquid clock widget for Mac OS X Dashboard
JavaScript
1
star
35

ecs-ami-metadata-endpoint

A CloudFormation template that helps you setup your own endpoint for fetching the ECS AMI metadata
1
star
36

greeter

Sample code for three microservices that construct a greeting
JavaScript
1
star