2016 01 May

Breaking Ajax Poll

By anand.pathak

Jessy : “Ajax polling became the granda's way to make thing real time ! Why aren't we doing   using this time ?”
Walter: “Look bro, I know you wanna do things from new way but you forget we don’t have time to experiment with it ! clients want it by tuesday and today is friday, so technically I don’t have more than 1 day uhh..(watched his watch) 5 hr and few minutes and I am not gonna ruin my weekend just because you want to do it  with your cool way and apart from this,  ajax poll is tried and tested way for us, it worked on the past. I know it takes more time to serve request  and uses more resoures  compared to your way but we are atleast  sure it will work on the otherside  I am not sure whether we  with able to complete it with way”

Jessy : “Fine ! I can’t say more because I also haven't tried it but still I feel we can give it a shot. If we see I can achieve through  then its great otherwise I am ready to sacrifice my weekends :( ”
Walter : “Cool  fine by me ! you have worked on node.js earlier ? right ”
Jessy  : “Yeah man !”
Walter : “then  it will be better for me to work on other stuff while you try your crazy things ! ”
Jessy  : “Okay”
Walter : “Lets make it clear what we will do.” 
Walter: "I am gonna work on twitter API and count the tweet which carries the required  keyword and then will  write the count in DB.. all you need to do is whenever I write in DB you trigger a event and send the new value to all the browser.  ”

Jessy  searched a lot about how would he trigger event when a write is been called and it took a lot time to him to get anything … coming back jussy says 
Jessy : “Hey bro,  I got a problem ! let's  do it another way ? I don’t find a way to trigger event when you write on DB so we have two option either do polling on DB or keep a file which you modify if you write on db and I will trigger even when that file is modify”
Walter : “ cool ! look you don’t have the time and  both seems fine to me but heart say for file modify because I don’t how how often will get tweets ”

Jessy  : “Okay ”
Just after 40 min ! jessy called the Walter  and said 
“Bro ! It’s  done from  my end ! ”
Walter: “What ! seriously ? No you kidding  !  show me the code man, can’t believe it  ” 


var fs = require('fs')
var events = require('events');
var eventEmitter = new events.EventEmitter();
var config= require('./_config.js')
var mysql= require('mysql');
var app = require('http').createServer()
var io = require('')(app);


var con = mysql.createConnection({
  user: config.user,
  password: config.password,
  database: config.dbname
    		console.log('Error connecting to Db');
  		console.log('Connection established to DB');


fs.watchFile(config.filename , (curr,prev)=>{
		if(curr.mtime > prev.mtime)
var row
var fetchDB= function(){
	con.query(config.query, function(err,rows){
 		 if(err) throw err;


	con.end(function(err) {
  			console.log('connection clossed');
var client=0;
io.sockets.on('connection', function (socket) {
			con.query(config.query, function(err,rows){
 			 	if(err) throw err;

Walter: “Oh man  ! Finally you learnt to make pure !(99%) but can you explain it ? ”
Jessy : “Look, the code is simple and self explanatory still If you want I will ”
“What it does is watching the file for change in  fs.watchfile() method.. When the file is been modified it triggers ‘write’ event which calls fetchDB method. In fetchDB method query is been executed and the data is broadcasted to all the client listening to the socket. socket is already  created and it listen to the port 8888 at the top . At the client side code all we need is include socket.js for browser side  and write the event listener for the event triggered from this code ! ”


  var client = io.connect('http://server_DNS:port');
    client.on('value', function (data) {


Walter : “cool  ! It is easy, why did  I used ajax poll earlier,  but still I have a question what the heck is in require('./_config.js') ?  ”
Jessy  : “Oops ! I forgot to tell you this.. Its the configuration file ”


var _config= {
	filename: "cool.calm",
	query:"SELECT * FROM table_name",
	initial: "0"

module.exports = _config;

Walter: “Do me one more favor tell me how to setup it ? ”
Jessy : “Okay ” 
“All you need is to do is to install two dependencies”

$ npm install mysql
$ npm install

Walter: “This is coooool ! man “


Featured blog


Breaking Ajax Poll

Jessy : “Ajax polling became the granda's way to make thing real time ! Why aren't we doing   using this time ?”

Read More

Say no to captcha

Say no to captcha - Various Spam Protection Methods

Maintaining high traffic websites have their own merits and demerits, the most annoying thing about them is SPAM.

Read More

Install latest Drush 7 version

How to install latest Drush 7 version? So you can use drush with the brand new Drupal 8.


It is interesting how when you are trying to do something new in drupal you land up learning (and doing) other newer things!

Read More

Install Memcache on your ubuntu server

Install Memcache on your ubuntu server & make your drupal faster

NOTE: This post explains setting up memcache on Drupal 6. We will very soon follow up for Drupal 7. Why do I need memcache for my drupal site?

Read More