Click here to Skip to main content
15,881,248 members
Articles / Web Development / IIS

HTTP Push from SQL Server — Comet SQL

Rate me:
Please Sign up or sign in to vote.
4.91/5 (55 votes)
27 Nov 2012CPOL10 min read 203.4K   4.2K   169  
This article provides an example solution for presenting data in "real-time" from Microsoft SQL Server in an HTML browser. The article presents how to implement Comet functionality in ASP.NET and how to connect Comet with Query Notification from SQL Server.
// Copyright (c) 2010 Daniel Wojciechowski 
// E-mail: at gmail.com daniel.wojciechowski
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
// Called when async postback ends
function EndRequestHandler(sender, args)
{   
    // this function is run after an Ajax partial postback occurs

    if(isPolling == 0)  // this "if" prevents from runing two requests
        longPolling();
}


function addLongPollingError(type, msg){
    $("#DebugLabel").html(
        "<div class='msg "+ type +"'>" + msg +"</div>" +
        "<button type='Button' onClick='longPolling()'>Connect again</button>"
    );
}

var isPolling = 0;
function longPolling()
{
    // prevent from runing two requests:
    // It is now commented because it is currently done in EndRequestHandler
    //if(isPolling>0)
    //    return;
        
    isPolling++;
    $.ajax({
        type: "GET",
        url: "CometAsyncHandler.ashx?waitTime=60", // one minute

        //async: true, 
        cache: false,
        //timeout:12000,

        success: function(data){ 
            isPolling--;
            if(data == "NEWDATAISAVAILABLE")
                RefreshData(); // this function is generated by using RegisterFunctionToPostBack()
            else if( data == "TOOLONG-DOITAGAIN" )
                setTimeout("longPolling()", 0 );
            else
                addLongPollingError("error", "Error on server side. Received data: \"" + data + "\"");
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            isPolling--;
            addLongPollingError("error", textStatus + " (" + errorThrown + ")");
        }
    });
}

$(document).ready(function(){
    longPolling(); // Start the initial request 
});

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Poland Poland
I graduated from the Jagiellonian University. After defence of master thesis (link to presentation) I have started professional programming in C and C++ using OpenGL. After that, my Company gave me a chance to switch to .Net framework and C# and I have betrayed my passion for computer graphics. Now I'm C#, SQL and sometimes ASP.NET programmer.

Comments and Discussions