Blog

Node.js and Socket.IO

Posted by On Dec 06, 2012 In Node.js, Protocol

Node.js is pretty awesome! I encountered the asynchronous JavaScript based web server platform when working on a specialization about (network) communication. As a demo I wrote a basic web server vote/polling system where people can vote on one page and see the polls rise and fall on another page. Data sending and retrieval was almost on the fly available through the use of Socket.IO’s “websockets”.

Of course this is a demo which means that there is still (a lot of) room for improvements. I’ve used to save the statistic values because it was a mandatory part of the assessment.

Working

So how does the Node.js application work? To start the application index.js has to be run trough node.js command prompt.

flow chart 1

When a resource is requested the following process starts. Pathname is everything that comes after localhost:1337/ ( localhost:1337/[pathname]).

flow chart 1

When a websocket connection is established.

flow chart 3

When the webserver receives new form data.

flow chart 4

The HTML pages

flow chart 5

flow chart 6

Conclusion & Resources

I hope this post gives a nice overview of the vote/poll application. Node.js and Socket.IO are pretty cool technologies that I will surely use in future projects!

The Node Beginners Book was a very useful resource for getting started with Node.js.

5 Comments

  • Daniel on Sep 25, 2014 Reply

    Hi Barry!

    I have a question. I am new to Node.js. You said if the values are changing (after a vote) the graph will also going to change. But the graph changes only when I refresh the page. What is the problem?

    Best regards,
    Daniel

  • Moises Serrano on Sep 04, 2014 Reply

    Great post! :D

  • Ed on Oct 30, 2013 Reply

    Thanks Barry. I’ll give this another try tonight.

  • Ed on Oct 30, 2013 Reply

    Great example. I am having a little bit of difficulty getting it to write to the json file. Here is the error. Are there any node modules needed other than the serial.io? Any idea why the failure below?

    Thanks!

    • admin on Oct 30, 2013 Reply

      You only need Socket.io to run this demo. After running the demo at my work computer I’ve encountered the same error. I think it has something to do with an updated version of Node.js. Anyway I’ve also found a solution.

      In statistics.js change the following.