www.webdeveloper.com
Results 1 to 11 of 11

Thread: jQuery ajax question

  1. #1
    Join Date
    Dec 2010
    Posts
    60

    jQuery ajax question

    Here is my code

    Code:
    (function poll(){
        $.ajax({ url: "http://pipes.yahoo.com/pipes/pipe.run?_id=826a9552b727729c2795ece9c97aab3f&_render=json", success: function(data){
            //Update your dashboard gauge
            for(var i=0; i < data.count; i++){
    			$('body').append('<span>'+data.value.items[i].title+"</span><br />");
    		}
    
        }, dataType: "json", complete: poll, timeout: 5000 });
    })();
    Now I know that this keeps inserting the same data over and over again, but my question is, every time the function runs after thetimeout, does the site bandwidth get eatin up by this user going to the site, or does it make the user download the data, and I just display it. If it runs all day, will the it eat up lots of my sites bandwidth?
    MY BLOG:
    HTML Code:
    <a href="http://teststuff.freehostingcloud.com">click here to read my game progress</a>

  2. #2
    Join Date
    Oct 2013
    Location
    Boston, MA
    Posts
    13
    Unless you own yahoo.com it won't be your servers that get pinged every 5 seconds.

    To go beyond your question a little, yes each ajax request is a full request back to the servers so the polling function you have there will make 1 request every 5 seconds for each user currently running that script. If you have tens of thousands of users all running this then it could definitely slow you down. It all depends on a few things like the server resources you have and how efficient your site is.

    There are multiple alternatives to "polling" like long polling which keeps a connection open until there is data to send back. It all depends on what you need to do.

  3. #3
    Join Date
    Dec 2010
    Posts
    60
    So here is my current code

    Code:
    function getBreakingUs(){
    		
    		breakUsDelay=setTimeout(function() { 
    		$.ajax({ url: "http://pipes.yahoo.com/pipes/pipe.run?_id=9071f6d2b4c14fb3a9cb294ea7dbbd5d&_render=json", success: function(data) { 
    				
    				if(techstatus.status ==0){
            			for(var i=0; i < data.count; i++){
    						var links = data.value.items[i].link;
    						
    						var fullUrl = links;
    						
    						var minUrl = fullUrl.split('.')[1];
    						if(minUrl == 'google'){
    							minUrl = fullUrl.split('.')[3];	
    						}
    						
    						$('#'+box).append('<div class="article"><img alt="logo" class="logo" src="images/logos/'+minUrl+'.png"><span class="story"><a href="'+links+'" target="_blank">'+data.value.items[i].title+"</a></span><div class='clear'></div></div>");
    				}
    				techstatus.status=1;
    			
    			} 
    		
    		else if(techstatus.status==1){
    			var story=$('#'+box+' span');
    			
    		
    			
    			
    				if(data.value.items[0].title != $(story[0]).text()){
    				
    					var links = data.value.items[0].link;
    						
    						var fullUrl = links;
    						
    						var minUrl = fullUrl.split('.')[1];
    						if(minUrl == 'google'){
    							minUrl = fullUrl.split('.')[3];	
    						}
    						
    				
    				
    					$("<div class'article' style='background-color: pink; font-weight: bold;'><span class='story'><img alt='logo' class='logo' src='images/logos/"+minUrl+".png'><a href='"+links+"' >"+data.value.items[0].title+"</a></span><div class='clear'></div></div>").insertBefore(story[0]);
    					$('#'+box+' .story').first().css('display','none');
    					$('#'+box+' .story').first().fadeIn();
    				
    				}
    			
    		}
    				
    				}, dataType: "json", complete: getBreakingUs 
    			}); 
    		}, 5000); }
    According to a source, this is a long polling function, but how can it be long polling if the function is repeated?
    MY BLOG:
    HTML Code:
    <a href="http://teststuff.freehostingcloud.com">click here to read my game progress</a>

  4. #4
    Join Date
    Oct 2013
    Location
    Boston, MA
    Posts
    13
    Long polling isn't a client side feature, it's server side. Basically what long polling does is keep the connection open to the server until there is data to be sent back to the browser. Once there is data the server sends it back to the client but does not end the connection.

    Here is a good article on the history of polling and why long polling is efficient:
    http://techoctave.com/c7/posts/60-si...ipt-and-jquery

  5. #5
    Join Date
    Dec 2010
    Posts
    60
    Quote Originally Posted by haggy() View Post
    Long polling isn't a client side feature, it's server side. Basically what long polling does is keep the connection open to the server until there is data to be sent back to the browser. Once there is data the server sends it back to the client but does not end the connection.

    Here is a good article on the history of polling and why long polling is efficient:
    http://techoctave.com/c7/posts/60-si...ipt-and-jquery
    This is the site i used. Although his long polling code in the article is not correct, it causes the function to run over and over and over again really really fast. Not to sure that is what it's supposed to do. It looks like someone commented on that and he revised his code in the comments section. So when I inspect the the network on the page using mamp, it basically says every time the timeout function runs, I download the json article again. is this correct? if so, how is it keeping the connection open?
    MY BLOG:
    HTML Code:
    <a href="http://teststuff.freehostingcloud.com">click here to read my game progress</a>

  6. #6
    Join Date
    Oct 2013
    Location
    Boston, MA
    Posts
    13
    If it is the request is completing very quickly and the ajax complete() function is being called constantly that means the server is not keeping the connection alive. It is satisfying the request then closing the connection.

    It is not reccomended to run long polling setup on Apache because it is not amultithreaded request queuing setup. Each request to an Apache web server consumes an apache worker thread so if you are keeping connections open for every client then you will quickly run out of worker threads.

    What web server are you running?

  7. #7
    Join Date
    Dec 2010
    Posts
    60
    Quote Originally Posted by haggy() View Post
    If it is the request is completing very quickly and the ajax complete() function is being called constantly that means the server is not keeping the connection alive. It is satisfying the request then closing the connection.

    It is not reccomended to run long polling setup on Apache because it is not amultithreaded request queuing setup. Each request to an Apache web server consumes an apache worker thread so if you are keeping connections open for every client then you will quickly run out of worker threads.

    What web server are you running?
    Yep, right now I'm running on MAMP on my mac. It will be moving to a linux server once done that runs apache. As you know MAMP runs apache. Is the way I have it set up long polling? It seems to redownload every time 5 seconds goes by.
    MY BLOG:
    HTML Code:
    <a href="http://teststuff.freehostingcloud.com">click here to read my game progress</a>

  8. #8
    Join Date
    Oct 2013
    Location
    Boston, MA
    Posts
    13
    I found this article on long polling which provides another good explanation: http://dsheiko.com/weblog/websockets...s-long-polling
    Think of it this way, you send a request to the server. The server continuously checks for a certain state change (it can be anything) and does not return the response until something changes. Once the server returns the response, the client fires off another request to start the process all over again. The important thing to understand here is that the server should not send back a response until something has changed.

    As far as using apache goes it can be done but there is a downside. Apache is not always the ideal web server to use for long polling. In Apache you have to spawn a new worker thread every time a new long poll request comes in. This can add up very quickly and take your server(s) down. Here is the breakdown.

    Say Apache starts 30 worker threads by default.
    A client connects to your website. This will consume 2 worker threads. Worker thread 'A' will handle the page request and worker thread 'B' will handle the long poll connection
    Once the server processes the page request, worker thread A will be freed but worker thread B will continue to be dedicated to that single long poll connection

    This happens for each and every page request. You can see that when you are trying to serve thousands or tens of thousands of page requests that this can become a serious issue.

  9. #9
    Join Date
    Dec 2010
    Posts
    60
    Still not seeing how this keeps the connection "open" because every time the interval passes, it downloads the url again.
    MY BLOG:
    HTML Code:
    <a href="http://teststuff.freehostingcloud.com">click here to read my game progress</a>

  10. #10
    Join Date
    Oct 2013
    Location
    Boston, MA
    Posts
    13
    The article I put above explains it well. It that example it checks for some kind of change server side. The web server does not send a response until something has changed. That is what keeps the connection open until a change has occurred.

    Does that make sense?

  11. #11
    Join Date
    Dec 2010
    Posts
    60
    Ok, I feel like the last site you gave doesn't explain much of anything. I'm not using PHP I'm using javascript/jQuery. Looking at this site:

    http://techoctave.com/c7/posts/60-si...ipt-and-jquery

    The technique he calls long polling, or "keeping an open connection", how is it keeping an open connection? If i use the code and look at my inspector, there is no open connection, it's just redownloading the same file over and over. Am I just not understanding the code?
    MY BLOG:
    HTML Code:
    <a href="http://teststuff.freehostingcloud.com">click here to read my game progress</a>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles