Using Azure Functions, Node.js and Cosmos DB to create a game leaderboard

In the previous blog post, we described some thoughts on how to design a game leaderboard and how to represent it programmatically. In this one, we are going to discuss about a new open source project of mine called AzureFunctionsNodeLeaderboards-Cosmos. As the name implies, this project is about game leaderboards using Azure Functions with Node.js and Cosmos DB.

Software/architecture

AzureFunctionsNodeLeaderboards-Cosmos is a starter kit for a game server’s leaderboard that uses the following Azure services:

  • Azure Functions: Functions is Azure’s serverless Platform as a Service. It is an event-driven, scalable, on demand and Pay-As-You-Go platform.
  • Cosmos DB: A globally distributed multi-model database service. Cosmos DB has been designed to operate with minimal latency and uses NoSQL technology natively. You can access Cosmos DB using various APIs, like SQL API, Table API, Cassandra API, Gremlin API and MongoDB API (which we’re using for this project).
  • Application Insights: This service is used to monitor our leaderboard service for performance issues, errors etc.
  • Unity: A Unity sample client is provided along with a corresponding C# SDK to access the leaderboard API.

On the architecture side of things, the API itself is written in Node.js and uses the Express framework (via the azure-functions-express package) to expose its operations via REST APIs. We’re also using Cosmos DB API for MongoDB to host the leaderboard and the Node.js app communicates with Cosmos DB via the Mongoose ORM framework.

functions[1]
Project’s architecture on Azure

Deployment

You can deploy the project to your own Azure subscription via one-click ARM deployment. Alternatively, you can deploy the project via a Docker container. Kubernetes deployment YAML files are also included if you wish to deploy the project to your K8s cluster.

Local development

You can fork/clone the project and work on it locally. You can use Azure Functions Core Tools or a simple ‘node index.js’ to get the API running on your local machine. Don’t forget to set the required ‘.env’ and ‘local.settings.json’ (depending on how you opt to run it) environment variables.

Testing/logging

When deploying on Azure, the ARM template generates an Application Insights instance that monitors your project. When running locally, everything is logged to standard output (console). You can also use ‘npm test’ to initiate mocha test runner that uses chai to verify that all your API calls are working as intended.

Supported API operations

The API supports many operations, from posting a new score to getting top scores, daily scores etc. All (apart from the new score creation) are easily accessible via GET HTTP calls.

Authentication

Azure Functions is based on Azure App Service which offers a mechanism which you can use for authentication of your service with well-known providers such as Azure Active Directory, Facebook, Google, Microsoft Account, and Twitter. You are encouraged to use one of these or implement your own functionality. Read here for more details on how to use authentication for the API.

More information

Apart from project’s README file that contains an overview about the project, there are more documentation related files included in the ‘docs’ folder, you are encouraged to read them:

  • FAQ: answers to some common questions about the project
  • Operations: detailed explanation of each one of the API method calls/operations
  • Technical Details: here you can see description about various technical details of the project

Issues/Pull Requests

If you encounter any issues, feel free to raise an issue on the GitHub repo. I’m also open to PRs, you are encouraged to add additional functionality to the project. Keep in mind, however, that I’d ideally like the project to remain as lightweight as possible.

Thanks for reading this far! Please visit the project’s page on GitHub and let me know what you think!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s