Jan 26, 2015 - A Simple Snake Game

Comments

I talk to some people about the fact that “I’ve never written a game”, meaning that I’ve never written code with a main() loop that draws some sort of graphic and interacts with user inputs. I’ve decided it’s time to say goodbye to those days, so I pulled out a easy tutorial and wrote a Snake Game using HTML5 canvas and Javascript.

It was a lot easier than I thought; got it done in about an hour, with less than 200 lines of code.
It’s pretty fun to write your own game, and see it in action :)

Here I do have some inefficiencies that I may want to avoid when developing a higher performing game.

  • Frame rendering: I am drawing the entire background and re-drawing the objects on every frame. I believe there is a more efficient way of drawing the graphics on the canvas - for example only drawing the delta.
  • Snake collision detection: In every frame I am comparing every cell of the snake with the position of its head in order to check for collision. I’m doing this through a simple for loop. This can get pretty expensive when the snake becomes very long. Maybe I should have a more efficient way of searching through the array, for example setting the x and y dimensions as a key, and searching for the key. I’m sure key searching is a lot faster.

You can play it here:
http://ashiina.github.io/lab/snake-game/snakegame.html

Here’s the code:
https://github.com/ashiina/snake-game-demo

Jan 21, 2015 - Fetching Data with Amazon Lambda & SQS

Comments

aws-lambda-datafetcher-sample

Sample (proof of concept) for data fetching with Amazon Lambda & SQS

https://github.com/ashiina/aws-lambda-datafetcher-sample

Concepts

I wanted to see if I can develop a way to query and get data from a database, without directly querying the database from the client. I wanted to try to implement a data retrieval request-response model, just using AWS’s fully managed services.

But since Amazon Lambda does not have a way to return data after invokation, it seemed extremely hard, if not impossible, to create such a thing.

I thought that if I use it together with SQS long-polling, it may be possible to implement an asynchronous data retrieval request.
So this is a proof-of-concept showing how that can be done.

Implications

Being able to fetch data just with Lambda & SQS means that entire application backends can be made just with this technology and a database; Without managing any EC2 instances (a 2-Tier architecture, in AWS terms).

This proof-of-concept only uses fetch_id as a primary key to retrieve data, but this idea can be extended to executing more complex queries, or even passing raw SQL queries over Lambda into RDS.

Limitations

Since this fetching goes through multiple HTTP request, the response time is not optimal (3~5 seconds on my environment).

How it Works

  1. Set up:
  2. Create a SQS queue with the name “user-queue-{user_id}”, where {user_id} will be the unique ID of the user who will retrieve data.
    (In a real life setting, each users’ SQS should be automatically created upon the creation of that user.)
  3. In index.html, set LambdaDataFetcher.queueUrlPrefix to the queue URL, without the {user_id} part.
    (This is so that you can dynamically create queueUrls for each user.)
  4. Create a Lambda function with the dataFetchLambda.js included. (In a real life setting, create an external database, whether RDS or DynamoDB, and store data there. Since this is a sample I do not have an external database; Instead I just have a JSON that acts like a database)

  5. In index.html, LambdaDataFetcher.fetchData() is called with two arguments - fetchId and userId.
    fetchId will be the primary key ID of the data you want, and userId will be the unique ID of the user (same as the ID in the queue URL).

  6. Inside fetchData(), first a sqs.receiveMessage() will be executed. This is a long-polling request for SQS, and its URL will be the queue URL created earlier with the userId.
    The timeout is arbitarily set at 10 seconds.

  7. Next, a lambda.invokeAsync() is executed on the function created earlier. As an argument, a custom event data is sent with three parameters:
    • fetch_id : The fetchId passed in as an argument (primary key of the data you want)
    • sqs_queue_url : The queue URL which you are currently long-polling on
    • nonce : A random nonce created upon this unique request
  8. Inside the Lambda function, it will look for the data with the fetch_id and stringify its data as a JSON string.

  9. The Lambda function will then execute a sqs.sendMessage() to the sqs_queue_url which was passed in. It will have two parameters in the Message Body:
    • nonce : The exactly same nonce passed in the event data
    • data : The JSON string of the data
  10. Back at index.html, the SQS which was long-polling will then receive the message sent by Lambda. It will check if the nonce is exactly the same as the nonce sent upon invokation, to check that it hasn’t received a different object than the one it requested.

  11. If the nonce matches, the data is considered valid, is returned to the callback function of fetchData().

  12. Finally it will call sqs.deleteMessage() to delete the message it successfully received.

Jan 20, 2015 - Lambda - Thoughts on some use-cases

Comments

I was exploring some use-cases of Amazon Lambda. Being able to cut out EC2 from a techonology stack is very potent in cutting down operations cost and reducing complexity. Liberating developers from having to manage Linux boxes will be a groundbreaking progress in web application development.

The thing with Lambda is that it is an event-driven code execution paradigm, so I can SEND input data(events) into it to have it perform operations, but I cannot GET (fetch) data. And since a typical application required CRUD(Create-Read-Update-Delete), not having the Read is not enough.

… Well, is that so? Yes, it is asynchronous, but what if I can send a custom event of something like:

{
	"action": "get",
	"id": "100",
	"notify_id": "12345"
}

Inside the Lambda function, I can fetch some data from some database(RDS or DynamoDB) with the id 100. Since it is asynchronous I cannot return the request, I can have the client be long-polling on an Amazon SQS queue. Then just have the Lambda function publish an event onto the SQS queue, and the client can do whatever it should upon receiving that.

So there, we can create a request-response flow using Amazon Lambda and SQS.

The issue then would be that each user will have to have their own SQS Queue, so you will be managing many, many queues. But as for now I believe there are no limits to the number of queues you can create on SQS, so it shouldn’t be that much of a problem.

This seems fun, so maybe more on this a little later. I would want to try and implement the request-response example just as a Concept of Proof.