Node.js realtime Mapping


So imagine the case where your boss asks you to provide a map with realtime data being displayed.

Not only for internal use, but open for all to see.

The first thing that you will think is that this will take down your server having to send requests for new data every couple of seconds for each user.

This is when Node.js comes in very useful as it listens for new data rarther than having to request for new data.

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

My example is built just on my development environment so this is a starting guide to get you going.

Download the installer package you require from Node.js and install.

My full working example is available at https://github.com/glynrob/Node.js-mapping

So lets get started:

Firstly you need the node.js server running. So for windows you open up command prompt and go to the folder location that you have downloaded my example from.

Once in that directory type in node server.js

If you do not see “Server at http://127.0.0.1:8080/” in the command window then you may have not installed node.js correctly so check your setup.

You do not need apache running as you normally would with PHP as node.js is the platform that you are now using to run this code.

The port for my example is on 8080 so if you go to your browser to the URL http://127.0.0.1:8080/ you should see a map of the UK with a single button ‘Add Point’ at the top.

You can change the port in the server.js file on line 2 if you wish to run this example from a different port.

Once you have this example working you can press the ‘Add Point’ button and you will see a new point marked on the map.

Impressive? Well you can easily build a javascript version that randomly pinpoints on a map, but wait…

what if someone else was on the website at the same time.

Open a second browser window side by side and press ‘Add Point’ again.

It displays the new point on both screens. This is Node.js at work pushing the new data to all pages giving realtime data without the browser having to request the page every few seconds.

So now if you wanted to see a realtime list of all traffic hitting your website, small amends to push each users long and lat data to node.js would do that for you.

So download https://github.com/glynrob/Node.js-mapping and see how quick and simple it is to implement.

2 comments for “Node.js realtime Mapping

  1. mike
    March 4, 2013 at 1:11 am

    How do I use this to make a site similar to fixposit.com

  2. GlynRob
    April 15, 2013 at 3:39 pm

    That should be simple once you have my example setup.
    Change the add point button to get a new location from the navigator.geolocation javascript call.

Leave a Reply

Your email address will not be published. Required fields are marked *