Take Lua to the Clouds and Luvit - Archived

Luvit Lua Moon on OpenShift Picture

What is Luvit?

Luvit takes the idea of a non-blocking IO from Node.js and replaces JavaScript with Lua. Lua is a scripting language that you usually do not hear about, but it’s pretty familiar to many of us. How is that? Because Lua is very often used in games for scripting and providing way to modify and automate tedious tasks – the most well know use of Lua is probably World of Warcraft. The Luvit project describes itself as

Luvit is an attempt to do something crazy by taking node.js' awesome architecture and dependencies and seeing how it fits in the Lua language.

To get started with the Lua language you can read an on-line e-book.

Why Lua?

Are you asking why to use Lua and Luvit instead of Node.js with JavaScript? Well, I was asking the same thing, but there are several valid reasons.

Lua is much faster. As Luvit is still in development and there is not so much money being spent on the optimization as on Node.js, it manages to be several times faster.

Lua also provides support for coroutines. Coroutines allow you to pause the execution of the program, i.e. save the current context and resume later. Useful? As hell! If you had worked with Node.js you definitely had to find yourself in the so-called callback hell. Coroutines allow you to get rid of these callbacks and instead of calling one, just pause and resume the execution of the program.


Create OpenShift application

rhc app create -a $name -t diy-0.1

and enter the directory

cd $name

Add this repository as new remote

git remote add upstream -m master git://github.com/openshift-quickstart/luvit-openshift-quickstart.git

and pull locally

git pull -s recursive -X theirs upstream master

finally, deploy to OpenShift

git push origin master

Now, your application is available at



You can change the Luvit version in .openshift/action_hooks/start. The quickstart automatically detects whether the requested version is installed and in case it’s not, the script installs it.

Code explanation

Let’s just take a quick look on the sample Luvit application in the repository. Those who are already familiar with Node.js will find the code somewhat familiar.

local env = require('env')
local http = require("http")

First we need to require some libraries. “env” allows accessing the environment variables. “http” is part of the Luvit project and provides the HTTP related functionality.

print("Starting server to listen at "..env.get("OPENSHIFT_INTERNAL_IP")..":"..env.get("OPENSHIFT_INTERNAL_PORT"))

Print some debugging info, to know where the application will bind. Most important thing here to recognize is, that string concatenation is done by using “..”.

http.createServer(function (req, res)

Create a new HTTP server and define anonymous function that will handle incoming requests.

  local body = "Hello world\n"

Define local variable called “body” and fill it with the most well-know programming string followed by a new line character.

  res:writeHead(200, {
    ["Content-Type"] = "text/plain",
    ["Content-Length"] = #body

By calling the “writeHead” method on the response object the applications sends the HTTP response header back to the browser, i.e. the status code and the headers. The application defines the response as plain text, so the new line character will be recognized in the browser. “#variable” construct allows return the length of strings and tables, in this case the length of the string in variable “body”.


Finish the response, write everything to the client, flush and close the connection if needed.

end):listen(tonumber(env.get("OPENSHIFT_INTERNAL_PORT")), env.get("OPENSHIFT_INTERNAL_IP"))

Here we call “listen” function on the object representing the HTTP server to bind the server to specific IP address and port. The required information is provided by OpenShift through environment variables.

print("Server listening at http://"..env.get("OPENSHIFT_INTERNAL_IP")..":"..env.get("OPENSHIFT_INTERNAL_PORT").."/")

Once the server is up and running, print debugging information message where it is listening for incoming requests.


And that is all, you can run Lua with Luvit in the cloud for free and enjoy all the goodness of Lua.

What’s Next?

Comments are closed.