Marcos Placona Blog

Programming, technology and the taming of the web.

Simple HTTP server with Dart – Setup

Reading time: 4 – 6 minutes

In this article, I will demonstrate how simple it is to create a simple TCP HTTP server with Dart.

We will start off very simple by just creating a simple server, that returns an HTTP status code 200. This will be the bare bones to a small server that starts by acting as a repeater, then moves onto logging each of the requests, and finally turns into a small chatting application.

We start off by importing the dart:io library into our project. This will allow us to create a socket that will later be used to serve our pages and endpoints. It’s important to understand that this dopes not work on a browser, so to establish sockets in this way, we need to build this as a console application that will act as our server and be running in the background.

We then decide which port and address we would like out application to run. For simplification purposes we will run this on (coz there ain’t no place like it) and port 5000, but feel free to use whichever values suit you best.

At this point, your code should look like the following:

Not much happening there so far.

We then setup our http server and use a Future to establish when the server has started up.

If we dissect the code above a little bit, all it is doing is binding a new HTTP server instance to an address and port, and because this is a blocking operation (meaning you wouldn’t be able to do anything else until this operation completed), we use a Future that will act as a callback, and will only return when an operation is completed. This means your application is now free to do anything else it needs to, even if the process of starting it was to take a couple of seconds to complete.

We obviously need to make sure we do something once the operation is completed. What we do, is assign the response to a variable (server). This is the context of our new process, and will allow us to interact with it.

At this point, we are only listening to connections, but not really doing anything about them. however, if you were to try to connect to it, at this point you would get a successful connection, but nothing that you sent to it would be taken into consideration, as our little server doesn’t have any handlers for messages.

We can quickly change this by telling our server to listen to incoming http requests, and return with an HTTP status code as such:

As you have seen, we’re just binding http requests to out http server by telling it to listen to it. That then gives us access to a variable called request, which we can the use to interact with the client by sending back a response.

Notice how I use method cascades to avoid repeating request.response time and time again (thanks to +SethLadd for reminding me of this).

We could have also used that to check if there were any cookies in the request, or to analyse the request headers passed with the request and build logic around it. More on the HttpResponse class can be found here.

In the next article, I will cover how we can turn this into an echo server, which will move us further towards the final project. Stay tuned.


Seth Ladd also quite rightly pointed out (in his comment bellow) that what I’m really talking about here is an HTTP Server as opposed to to a TCP Server (i.e. I’m talking about a higher level protocol that actually runs on top of TCP). The initial intent with this article was to make it a big write up about everything TCP, but then it turned out to be massive, and I decided to simplify and split it up in smaller articles, which for the purposes of this article took TCP slightly out of context. Fear not though, as the following code shows you how to accomplish it via TCP instead.


Making it snow with Dart

Reading time: 3 – 4 minutes

After my last example showing you how to create a matrix effect with Dart, I thought it would be cool, to follow up with another example that used the same concepts, but went into slightly more details.

It’s also a bit more complex than the last one, as on this one, we use a few more canvas properties, such as arc to generate our flurries, and beginPath to reset my objects so they’re actual flakes, and not a long rain trace (more on this later).

As per the previous example, I will be using canvas to display my animation.

The final product should look like the following:

Dart Snow

Although the animation will be way better than the gif image above.

I start by creating a class that will hold each of my flakes. Not much to explain here. This is only a PODO (Patent Pending) ;-)

I then go ahead and initialize some of the variables and constants that will be used by my application. I do it in such way I can experiment with values and get my application to behave in different ways depending on what values I choose.

Following, I have the brains of my little animation. Everything happens here, from canvas set-up, to flake creation to positioning. I have put together lots of different steps on the same method here. This is almost a tl;dr for code. I don’t want to bore anyone very semantic code. The idea here is to get people going. We all know separation of concerns and good semantics.

Notice we use beginPath every time we call the snowFall() method. This is to make sure the path for each of our flakes get reset, and don’t end up drawing its entire path ton the screen. If we hadn’t used that, we would end up with something like:

Dart Rain

This could be useful if you wanted rain instead of snow :-)

Finally on my main function, I do three things:

  1. I initialize my canvas object;
  2. create a pointer in memory for each of my flakes;
  3. make sure the animation runs every 30 milliseconds,

You can find the entire code published on my GitHub account, and obviously fork it and send me pull requests if you think you can make it any better.

Matrix effect with Dart

Reading time: 2 – 3 minutes

After my last post on Dart, I decided that I wanted to write something a bit more complex using the language. I had a lot of good feedback on it, but still felt I should try to push the language as much as I could, and see if it was a really enjoyable language to write code in.

So what better than a couple of hours on a Sunday evening to write some code and generate some animations?

I tried my best to be as descriptive as I could on the code. But it’s so simple… some things are better left unsaid.

Dart Matrix

The premise of the code, is the following:

  1. Generate a random character from a list of random characters. I used 1′s & 0′s as when I look at them…

    All I see is blonde, brunette, red-head

  2. I then move the numbers (drops) around the screen, and make sure they are always moving down, ’till they reach the end of the screen. Then it’s time to re-use them again at the top. #perfmatters after all.
  3. Canvas really gives me all I need to give my animation a transitional effect when it starts (it looks a lot nicer on the browser than the animated gif above).
  4. Make the awesomeness happen again and again every 30 milliseconds by only writing 60 enjoyable lines of code.

The code can be found on my GitHub account:

As I said, it’s pretty self explanatory, but it would be really nice to get someone else’s pair of eyes on it, and getting some advice on where I could improve it.

It seems to run on roughly 54fps on Dartium, but I’ve seen dips occurring sometimes.

A first look at Dart

Reading time: 7 – 11 minutes

A few weeks ago, I went to a Google sponsored event called Dart Flight School. The aim is to promote the language by doing a road trip and presenting use-cases and samples. The presentations were brief, and mainly focused on discussing the language’s functionalities, and its seamless integration with AngularJS (also maintained by Google)

I had a chance to look at Dart before, and was interested in finding out more things about it. Turns out the language (and platform) are pretty slick, and the development tool-set is pretty complete. The IDE is pretty good (and free), and their package management system is pretty similar to NPM’s in NodeJS.

Dart is also very (very, very) fast, and is in fact faster than V8 (VM used by NodeJS). A performance comparison can be seen below.

Dart vs V8 Engine

Data gathered from

It comes bundled with an out of the box converter to JS, which means you can write your entire application in Dart, and then convert it to JavaScript. I must say I was initially sceptical about this conversion, but upon looking on the performance page, it seems even after conversion, JS generated by dart still manages to be faster than V8 (purple line above). The JS converter is a Dart application written in Dart, which means you could then convert it to a JS application using itself…. bewildering eh?


From a language point of view, dart seemed to be very readable, and compliant with contemporary languages such as Java and C#. It is a class based language that allows you to fully use object orientation, and has some very nifty functionalities embedded in it, where you can for example define a method using shorthand syntax as such:

Data Structures

Its core library also provides you with Lists, Sets and Maps, which basically means no imports, as you get that straight out-of-the-box. So for example if you wanted to create a Map and iterate through it, you could simply do:

Unit Tests

A big part of writing great code lies with the ability to provide unit tests that will make sure your code remains awesome even after refactoring. With dart, you can as easily create unit tests by importing unittest

And obviously I could have just as easily grouped my tests in a single group to have them maybe organized in smaller units. I’ve also used shorthand syntax to define my tests here


Is supported by a library called Mirrors (enough said?). Though I feel I haven’t played with that enough to give you any better example than this.


According to Dart’s own website, a Future represents a means for getting a value sometime in the future. When a function that returns a Future is invoked, two things happen:

  1. The function queues up work to be done and returns an uncompleted Future object immediately.
  2. Later, when a value is available, the Future object completes with that value (or with an error).

Why would I use Futures instead of simply calling my expensive processes and waiting for them to complete?

I like to think that the people who read my blog know better than asking the question above, as it makes me feel fuzzy and warm. However if you thought of asking this question but were ashamed to actually do so, I will take you through it, and we will pretend this never happened.

It turns out, that if you do that, you will lock the thread until your application becomes responsive again, which can range from a few milliseconds to God forbid a few seconds. Meaning your users will stay put (or most likely leave) until you finish processing their request and show them some meaningful content. Think of it as “waiting ’till Friday is upon us”.

Seth Ladd gives a great example of the power a Future can have in your application

You can read more here.


Need I say anything? Need I?


In this day and age, you want to be able to work with a language that offers you integrated package management. I have worked with numerous languages in the past, and managing third party packages has always been the pain of my life. The first time I looked at Ruby, I immediately fell in love with its package management system. Granted some languages try to accommodate for this by adding capabilities to builders such as Gradle, Ant or Maven. But I digress….

Dart comes with a package manager called Pub, which means all you need is a yaml file inside your project where you can specify any libraries your project needs, as well as which version you would like to be locked to. that way, you only need to package your application with what it really needs, and all the external libraries will be downloaded on the time you deploy your project. This makes your application lean and easy to maintain.

A pubspec.yaml file would look something like:

Then run the following from terminal to download all dependencies:

Cool Factor

Dart is a cool language and very simple to pick up, but as with all the things in life (although you don’t always like to admit it), the known is always a lot simpler, and Dart strives to offer simplicity, which means if you have done any proper language in the past, you’ll be able to read and write Dart code with ease. Dart themselves state

“We did throw in some nice syntactic features such as this. constructor args and => for one-line functions, but we’d agree that Dart chooses familiarity over excitement”

Final Verdict

It’s very exciting to see such fresh language being supported and built by Google. In my opinion, the language offers everything the “cool kids platforms” offer and more. It also has capabilities that allow you to run Dart on the server side, client side and even natively on the browser. According to their documentation, the engineers behind Dart have an Android and Google App Engine integration on the back of their heads, and even though they say it’s not completely down to them, they mention on their FAQ’s you’d need to ask the team. But I’d say the fact they have thought of it is already half the battle won.

From a language perspective, I found nothing that would put me off writing code on it (and I’m pretty fussy about semantics). Instead I have found I genuinely enjoyed writing code in Dart, and was left with a nice after-taste after attending Dart Flight School. Even though I understand we had a much cut-down version of the even here in the UK.

From Here

New TeamCity agents the right way

Reading time: 2 – 2 minutes

TeamCity Logo

At work, I’m gradually moving our CI server from Hudson to TeamCity.

Nothing against Hudson really, but I feel that TeamCity is a much more robust CI Server when it comes to integrating with .Net. It allows you to publish artifacts from your builds, and has a killer integration between developers IDE’s and itself, which is amazingly helpful to help developrs make sure they’re not going to break the build… well before they break it.

But anyway, one thing that was slightly annoying me with TeamCity, is the fact that the build agents would often get disconnect, and all my builds would stay in a queue until I went and manually restarted the agents.

The “Build Agent Disconnected” quickly became very annoying, and by quickly looking up on Google, I found lots of people had the same issue, and while there were lots of responses or people claiming they found a solution to it, I never actually found anything of much use other than the screenshot this guy posted.

When you add build agents on TeamCity, you get the option of adding them as a windows service, or simply as an agent that runs with TeamCity. I had tried to add multiple build agents as windows services before, but for a very strange reason, I would always end up with only one agent no matter what I did. TeamCity’s documentation wasn’t much help to be honest, and I ended up figuring this out after a couple of hours of trial and error. So here’s how you do it properly.

Continue reading

« Older posts