You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Real-time serverless chat app on AWS with Go and Mantil
Mantil is a modern open-source framework for writing serverless apps in Go. It allows you to quickly create and deploy applications that use AWS Lambda over a command line interface.
This example shows how one can make a simple cloud native chat app with a serverless backend written in Go. Backend is built on AWS Lambda and uses WebSocket API and DynamoDB.
Through this example you will learn:
How to include a WebSocket API in Mantil and use it with the provided JS SDK
How to deploy a web application on top of AWS Lambda with Mantil
Prerequisites
This example is created with Mantil. To download Mantil CLI on Mac or Linux use Homebrew
In this example, we are using WebSocket pub/sub API to receive new messages on the client as soon as they are posted.
On the backend, we use the mantil.go Publish function to publish new messages to the chat-messages subject. The client then uses the mantil.js SDK to connect to the WebSocket API and subscribe to new messages.
Storing messages
To persistently store messages in this example we use Mantil’s built in key/value store which is built on DynamoDB. We simply create a store when initializing the API, and then use the Put and FindAll methods to store and retreive messages.
For a slightly more complicated use case you can refer to the todo example.
Deploying the application
Note: If this is the first time you are using Mantil you will need to install Mantil Node on your AWS account. For detailed instructions please follow the setup guide
mantil aws install
Then you can proceed with application deployment.
mantil deploy
This command will create a new stage for your project with the default name development and deploy it to your node.
Now you can output the stage endpoint with mantil env -u. This is where the website for this project will be availabe. The API endpoints can be invoked by specifying the function and method name in the path, for example $(mantil env -u)/chat/get.
Modification
If you want different behavior out of your function you can make necessary changes to your code in the api folder.
The client code is located in client/chat, to build it run:
cd client/chat
npm install
npm run build
This will build the static assets and copy them over to the Mantil public folder. Note that this is optional and only needed if you want to modify the client code. The project already contains prebuilt assets in public so you can start by deploying a new stage immediately.
After each change you have to deploy your changes with mantil deploy, or instruct Mantil to automatically deploy all saved changes with mantil watch.
To remove the created stage from your AWS account destroy it with:
mantil stage destroy development
Uninstall command mantil aws uninstall will clean up all created resources and leave the AWS account in the initial state.
Conclusion
Congratulations! With this example you learned how to create a simple serverless chat application with AWS Lambda and Mantil's WebSocket streaming implementation. It also uses persistent key/value storage that you can learn more about in todo example.
Have you got any questions regarding this tutorial or building serverless backends in Go? Feel free to reach us anytime at Mantil Slack.
About
Cloud native chat app with serverless backend written in Go on top of AWS Lambda using WebSocket communication.