How to Build a Chat App With Spring Boot and DynamoDB
Join the DZone community and get the full member experience.Join For Free
The goal of this article is to show the simplest chat implementation with one server and multiple clients based on Spring Boot and deployed to AWS.
For source code, you can refer to this link: https://github.com/acierto/aws-chat.git
First I'd like to tell amount the used API in the project:
- User first has to connect to the server with api GET call
/connect?name=USER_NAME. If your username is unique and you successfully connected, you'll get back a token. You will need this token to send messages or be able disconnect.
- To send a message, you need to send a POST call
/send?token=TOKEN, and in the body of the api call, enclose the message text. If the message is sent successfully, as a result you'll get
- You can check the entire history of all users by GET api call
- And when you would like to disconnect by sending GET api call
It's a very simple API and could be greatly improved, but the goal is not implementing a perfect API for chat, or handling all potential cases. Rather we'll see how to combine the things together. And keep the less code to easier grasp for beginners how it works.
Before going into details of the implementation, I'd suggest you to run that application locally, to configure so that you can experiment with what you read later.
First, you need to configure the AWS CLI at your computer. You can follow AWS documentation for that.
The most important that you have created
~/.aws/credentials having there something like:
~/.aws/config to specify your region
After that you need to:
- Checkout the project:
git clone email@example.com:acierto/aws-chat.git
cd aws-chat && ./gradlew :aws-chat-server:bootRun- this command starts the server. First time it can take a while to pull the docker image of Dynamodb. Wait till it starts.
- Then in a separate terminal window you can start a client (or multiple clients):
cd aws-chat && ./gradlew :aws-chat-client:bootRun.
Now let's see what is happening under the hood of the server part. The main class which boots the application is ChatServerApplication. You can see the annotation on that class @SpringBootAnnotation. This annotation does many things if you have a look of its sources
It runs auto-configuration of multiple modules and scans all packages. You can tweak it, by disabling some configurations or restricting which packages to scan. In this example it is not requires though.
Alone with boosting the application, we also setting up and running AWS Dynamo DB locally. So it's easy and free to work with it. The entire configuration for this part is done in build.gradle:
docker-compose.yml with docker image of DynamoDB
It's very nice to be able to browse the data and you can configure it with next steps:
npm install -g dynamodb-admin
Once you finish with that you can see something like this:
It also allows to manipulate with data, so really handy to make it up and running.
The configuration to the database is configured in application.yml, you can configure there the endpoint:
Configuration in Java is pretty simple for that. You can find many examples where also specified AWS credentials here, but it is not needed and actually an anti-pattern, as you will have to keep those credentials in your configuration files. Better to have them only in
~/.aws/credentials and make application read it implicitly:
To make the work with DynamoDb easy and minimum from the code perspective, here configured Spring-Data. It really simplifies a lot of internal things which you have to know to work with this database. I recommend to use it as well.
When application boots the first time, 2 tables are created:
There is no built in functionality in DynamoDB to create table only when it doesn't exist, and no API to check it just by name, so one of the solutions as here, to list all created tabes so far and check there.
When you create table in DynamoDB you have to specify the throughput
new ProvisionedThroughput(1L, 1L) , it's important as the bigger throughput you specify the higher pay check you'll get in the end of the month. In real application you have to tune it, so that it's optimal for users and your bill. First parameter is responsible for read throughput and second for write. The capacity is measure in Units. I advice to check official link to read about that https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html as there quite some theory how to calculate it. And if you are going to take AWS Developer Certification exam, you have to know it well.
Querying with dynamodb-spring-data is very simple, you have to only define interfaces:
All queries will be built for you.
One thing what is only required from you, to properly define entities for the database. Like let's have a look at user:
Only one field is mandatory for DynamoDb, is to define a hash key.
With a massive data you have to have a look at the usage of
DynamoDBIndexRangeKey. The difference between them only what kind of cache to use - global or local. As you might guess from names it depends where is the data is located, across different partitions or on the same partition. Though each of the option has own limitations which have to be considered carefully.
The client is pretty simple, it is also a spring-boot application which sends 3 messages and signs off. In resources there are 2 files added with the list of names and messages, which are picked randomly. If you will start playing with a big load of clients, could happen a name collision, but it's fine and user process will not be able to send messages.
In logs you will see similar to this:
The scheduler is running to send a message each second and after some interval it is stopped, user is disconnecting and the entire process is killing by
I also prepared a docker file to run it on EC2
You can also experiment with that by running multiple instances of clients on ECS, to make a load, changing the frequency and the amount of send messages, etc. Take into account that you will have to configure Dynamo DB on your AWS account first. But it will be quite easy by following GUI wizard.
Opinions expressed by DZone contributors are their own.