question

NandrianinaHarifidy-1537 avatar image
0 Votes"
NandrianinaHarifidy-1537 asked cooldadtx answered

Using event handler and View on Asp.net MVC

Hi.
I build a web application which get data from a server and display it.
Once we connect to the server it sends us event each time the data change.

Now I manage to get the data on a file, but I don t know how to display it on the View.
How to display event on a MVC View

Global.cs

 protectes void Application_Start(){
 // connect to the server and request data
 // server use event handler to send data
 server.Received+= server_receiced_handler
 }
    
 private static void server_received_handler(){
 using (StreamWrite fileAp=new StreamWriter(path:"D:\file.txt", append:true))
 {
 fileAp.WriteLine($"result is : {e.toString()});
 }
 }

Thank you very much

dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

Eventing doesn't really work well with web apps. HTTP is request-response. As soon as the server has returned a response it is done. While you can set up events they are not reliable in a web environment. For example suppose that the user sends you a "listen" request and you hook up to some event in the system. Any time after the response has been sent back to the client the site can shut down or be restarted. At that point your event listening is gone and the web client would not be any wiser.

You didn't clarify how clients "catch up" with the data that is already available so I'm going to assume they don't. I envision a service that sends something like real time stock prices and your view just updates. To get this to work in a traditional MVC app you're going to want to set up a timer on the client machine using Javascript (you can google for how to do this very common thing). The interval will be however fast you want to poll for new data. When the timer expires on the client it will make an API call (not an MVC one) back to the server to see if there is any new data. Again you can google for how to do this very, very common thing. The updated data, if any, will be sent back to the client and it then becomes up to your client framework (Angular, Vue, JQuery, etc) to update the UI with the new data.

For this approach to work the server is going to have to track the data. Every time an event is raised the data will need to be stored. Depending on your persistence needs and frequency it could be a database, file or even in memory. When the API is called it will likely need to know where the client is currently at in the data (perhaps a timestamp or unique ID) and then return whatever has happened after that. Of course if all clients see the same data then simply returning it all each time would be sufficient as well. It completely depends upon your needs.

A more elegant approach would be to use SignalR. This is designed for the situation you describe. In this mode the client will connect to the SignalR server. When an event comes into your code you would then send out the notification to any interested clients. This gives you the closest approximation to how a standard UI might work. However it requires that you understand how SignalR works so you need to read up on that as it is beyond a simple forum post. You'll also need to handle things like broken connections and whatnot but it is going to be the more efficient approach long term.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.