Telling jQuery to reload page when there is a server side event - c#

I have a page with a div and the elements within the div get updated from the server side periodically. For example, I have a progress bar in it. Currently, I'm doing jQuery.load using setInterval to periodically refresh the div on the page (every 3 seconds). I was wondering if I could achieve this using an event-based method rather than a timer-based. For instance, I could have a server event fire everytime the progress changes, and I could then increase the width of the progress bar on the page (and change other elements as well). Question is how can I tell jQuery to do a load when this happens? Is there a jquery onchange event or somethign that watches for page changes?

Polling could be done, but there are a better way: Comet servers. Basically the HTTP server allows long running requests which means that it do not send the response unless something have happened or the timeout is about to kick in.
Read about comet here: http://en.wikipedia.org/wiki/Comet_(programming))
A comet plugin for jquery: http://plugins.jquery.com/project/Comet
A more technical article: http://ajaxpatterns.org/HTTP_Streaming

This is simply not possible because the underlying communication protocol (HTTP) between server & client is request-response based. So it is not possible for server to notify client w/o client making request for it. In short, polling is the way to go.

Related

What is best & Secure timer for online test application in asp.net C# Ajax timer or Jquery Timer?

I am creating a new application in asp.net 4.0 which contains an online test module, and the application is supposed to handle 10-20k students at a time. Now my confusion is what should i use here, which should be:
Secure , I don't want kids to turn off javascript in browser and get unlimited time for test.
Which should not overhead the network traffic.
Which have a pause/start function with try limits.
Should fire the needed function on server side after time is complete (e.g. redirect to another page via c# function)
Any help and example code is appreciated.
Regards
Alok Sharma
FYI: I am not experienced with jquery.
If the security of timer is critical you should not fully rely on javascript code. I would recommend to keep the time when test is started for a specific user on both sides, server and client. Then implement javascript timer which periodically synchronizes with web server. When user completes the test and sends results to the server you should check if the timeout is elapsed on server side.
In additional I would add a logic in order to ban the possibility to open the page and start test without javascript enabled.

How to Replace traditional Ajax Polling with SignalR?

What I am trying to do with SignalR:
1- Setting the hidden field from a session UserID on page load and send this ID back to server's SignalR Hub to start the polling thread for given user.
2- Terminate the thread when user leaves the site.
Right now I am doing AJAX requests to server every 30 secs per user to check for new user messages.I Just want to replace it with SignalR. I am able to create the user level thread in a HUB when the users session is created by setting the hidden field on page load and then setting the session variable via ajax request so the new thread is not created for the same user again and again e.g page refresh. The thread is periodically checks after (15 sec) for newly arrived messages. The main issue is how do I terminate the thread created for a specific user when its session ends. Is this the right way to use SignalR ?
This doesn't sound like good design - polling is never good, not for this kind of problems anyway. It would be better if you could deliver the message instantly. Are you using multiple webservers? If not, then deliver the messages locally inside the application. If you are using multiple webservers, consider using something like RabbitMQ, Redis or similar to send messages between the servers.

Using C# to efficiently pull data from a webpage with changing sourcecode?

I have already put together code using the System.net.Webclient class to pull source code from a webpage, which I then use a string search on, to get specific information. This in itself works fine, but my issue is that the source code changes every few seconds, and I would like the data I have received to change accordingly. I understand that I could simply set up a loop to have this process repeat, but unfortunately my current code take a full 2.7 seconds to complete, and I would like to avoid this large lag time. In addition, I want to avoid spamming the webpage with requests if possible. I was thinking about a streamread that stays open, so that multiple requests wouldn't have to be sent, but I wasn't entirely sure how to go about this...
So to sum it up, is there a way that I can pull updating information from a website using the System.Net namespace in a manner that is both fast, and avoids spamming the website with requests?
I am afraid that HTTP protocol is not adapted to your real-time data refresh requirement. Other than polling with HTTP requests at regular intervals you cannot know whether the data changed on the server and get this fresh data.
For example the WebSocket technology is more adapted to those scenarios. Of course the data provider must implement it so that clients could subscribe to this live feed.
There's also another way to implement this feature over the HTTP protocol. It uses an iframe to implement long polling. Here's an example. The idea is that the server uses chunked transfer encoding and sends continuous streams of data to the socket. The client subscribes to this stream and is able to be notified of changes occurring on the server. Once again, it's a technology that must be implemented by the server side so that you, as a client, could take advantage of it.
If all that the server provides is data via HTML page you are doomed to do screen scraping by hammering this server with HTTP requests until your IP address gets black listed and denied access.

How to refresh aspx page from sql server

How to refresh aspx page from sql server.I am using asp.net,C-Sharp with SQL Server 2008.What i mean is i have table, say Table1.If any DML operation is performed (Update,Insert etc) to Table1,then my page,say Page1.aspx should autmatically get refreshed.I can't use timer for refreshing the page.I need to trigger the refresh from database.
Even though the server may be notified when data has changed, the real challenge is communicating those changes to the client in real-time without requiring a timer or user interaction.
You have a couple of options:
Your best bet is to use a WebSocket, which enables bidirectional communication between the client and server. This is the solution I would pick.
Here are some examples using WebSockets:
Building real-time web apps with WebSockets using IIS, ASP.NET and WCF
HTML5 C# WebSockets Server and ASP.NET Client Implementation
C# WebSocket Server
WebSockets in ASP.NET 4.5
WebHooks and WebSockets in ASP.NET
There are a few good libraries around too that will take care of most of the leg work. A couple to check out are WebSync and PokeIn. Both products offer decent documentation and community editions that you can download for free.
Here are some tutorials to check out:
WebSync Tutorials
PokeIn Basic Tutorial / PokeIn Advanced Tutorial
Use AJAX to poll for changes every X number of seconds. If changes are detected reload the page, otherwise do nothing.
You probably want to look into the SqlDependency object. This object will notify you of changes to a specified database query in real time. When your application receives a message from the database, you can simply refresh the page in your code-behind.
I wonder if you could adapt SignalR to send a message to the client to prompt a refresh?
This tutorial could get you started.

Push content to client after page is loaded

I wonder if the following is possible:
I have a dll that I have referenced in my web site. This dll makes a remote socket connection. The socket connection is waiting in the background and the dll reports back data through events after the socket has received some data.
The connection is opened during load of page.
Now, I would, for example, like to update a label on the page when new data has arrived.
I am not sure how this would work. I assume that I could set some kind of timer on page that updates a control but it does not seem "optimal" as I already call code behind through my events. "Optimally" the UpdatePanel or whatever updates the interface would wait for events and "update" when events has occurred and not based on time.
My question is - is this possible?
You can use techniques called "long pulling" or "web sockets".
There are libraries, like SignalR that can help you.
They generally use web sockets when client's browser supports and long pulling when not.
Using these libraries you can "push" commands/data from server to client's browser, just as you want.
Server cannot push the client, this is the rule how web works.
There are two possible ways to complete your task.
Put timer on the page and make requests on server too see, if something changed
Open long pulling connection between client and server, like facebook does. That listens
to the events and gets data from the server. It can be, for example xmpp or any other.
You should think about turning this around; rather than initiate the call from the code behind asynchronously and then update the client, you should deliver your original page to the client.
Once the page has been loaded client side, you can make an AJAX call to the server to retrieve the data you want - and display a little "I'm loading" symbol while this happens.
Page Methods are ideal for this.
The classic ASP.Net web page is supposed to last for the duration of the users request. When a postback happens, it effectively rebuilds the state of the call from the user's viewstate, session and any other state you've maintained. It never cares if the user goes away, for example (although it might feel a bit lonely).
Having it hang around for longer is problematic in a number of ways, however, you can implement client callbacks. However, although the server initiates this, the client manages the lifecycle, so it's analogous to page methods.

Categories