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 127.0.0.1 (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.

UPDATE

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.

 

4 Comments

  1. Hi,

    Thanks for the article! Have you considered using method cascades? You could do this:

    request.response
    ..statusCode = HttpStatus.ACCEPTED
    ..write(‘…’)
    ..close();

    Pretty cool!

  2. Oh, and I was wondering if you would consider clarifying that even though the post’s title says “TCP”, this article talks about HTTP. Which is totally cool. But it may be worth pointing out that Dart VM can also do raw TCP servers, too :)

  3. Marcos Placona

    April 1, 2014 at 23:02

    I knew about them, but completely forgot to use them. I’ll get the coffee updated for completion.

    As for clarifying on the HTTP & TCP, you’ve got a good point. I started the post willing to write an entire article covering everything, but soon realised the subject is very big, so thought I’d split it up in a few articles, which perhaps took TCP out of context. I’ll expand on that too :-)

    Thanks for your feedback Seth.

  4. Marcos Placona

    April 2, 2014 at 09:27

    Updated now +SethLadd. Let me know if you think the example is a bit more in context now (see update at the bottom)

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>