Clicking data from an ASP.NET MVC Controller on a view

I am creating a back end site that will have several front end widgets that need to be updated in real time.

Now I just have a loading method that fills all widgets with data when the page loads explicitly. My question is how to handle the real-time aspects of future updates.

I was thinking about having a few ajax calls that can request a service every second or so, and return the latest data, but that seems inefficient.

Is there a way to “ push ” data into a view from a controller?

+4
source share
4 answers

It depends on how often you need to update the data on the front panel. Most pages do not need to be constantly updated. I don’t know that there is a threshold of “best practice”, but I think a good starting point would be 15-20 second updates using Ajax. Make your Ajax calls fast and thin - maybe just return empty if there are no updates. If you need faster updates, look at what is called long polling . Long polling is basically where you call the ajax call to the server, and the connection is open until the data is sent. A long poll will take up more server resources because you will have open connections and threads while they wait for the data to be ready. With ASP.NET, you also have to worry about killing long polling streams, because by default these streams will not be killed when the browser closes the connection (for example, if someone moves away from the page.)

+1
source

maybe you can see this project: https://github.com/SignalR/SignalR

ASP.NET SignalR is the new library for ASP.NET developers, which makes it incredibly easy to add real-time web functionality to Applications. What is real-time functionality? This is the ability for your server code to click content on connected clients, as it happens in real time.

SignalR also provides a very simple high-level API for working with the server to the RPC client (call JavaScript functions in your clients' browsers from the server .NET code) in your ASP.NET application, as well as adding useful hooks to control the connection, for example. connection / disconnection of events, grouping of connections, authorization.

(Excerp from http://signalr.net/ )

Hope this helps.

+3
source

I think your best bet is to periodically poll the server:

 $(document).ready(function() { setTimeout("getUpdate()", 30000); function getUpdate() { // Make an ajax call here } }); 

This will request an update every 30 seconds.

+2
source

You can also use web sockets if running it in a browser that supports HTML5

+1
source

Source: https://habr.com/ru/post/1411133/


All Articles