locked
Best way to refresh changed data in View RRS feed

  • Question

  • User1314407993 posted

    I started reading an article describing the use of WebSockets via SignalR, but before I go too far, I wanted to ask what the best method for refreshing data on the View is. 

    Thursday, June 6, 2019 7:22 PM

All replies

  • User475983607 posted

    WilliamSnell

    I started reading an article describing the use of WebSockets via SignalR, but before I go too far, I wanted to ask what the best method for refreshing data on the View is. 

    The best method for displaying HTML depends on the application requirements.  SignalR uses JavaScript to update the DOM while MVC runs on the server.

    Thursday, June 6, 2019 7:37 PM
  • User1314407993 posted

    I'm confused about your answer - "best method for displaying HTML"? As far as I know, I don't need any special rendering for the HTML. That gets handled by the Razor view engine, I believe. What I need is to refresh the content of a view when the underlying data changes in the SQL database, and I'm not sure how that's performed. I have an ASP Core web app that accepts incoming WebHook notifications, as well as exposes an API to allow new entities to be saved via Instapage forms, so the entity list a user is looking at could get updated at any time. I need to listen to changes in the database and send a push to client browsers with the new data. 

    Thursday, June 6, 2019 8:14 PM
  • User475983607 posted

    WilliamSnell

    I'm confused about your answer - "best method for displaying HTML"? As far as I know, I don't need any special rendering for the HTML. That gets handled by the Razor view engine, I believe. What I need is to refresh the content of a view when the underlying data changes in the SQL database, and I'm not sure how that's performed. I have an ASP Core web app that accepts incoming WebHook notifications, as well as exposes an API to allow new entities to be saved via Instapage forms, so the entity list a user is looking at could get updated at any time. I need to listen to changes in the database and send a push to client browsers with the new data. 

    Your actual question is how to invoke a hub message from an external action like a WebHook?

    Anyway, since you are using SignalR I assume you'll update the DOM (HTML loaded in the browser) using JavaScript not a View. 

    Thursday, June 6, 2019 9:07 PM
  • User-474980206 posted

    web pages are a request made by the browser to the server which return html. the only way to update the html on page is for the browser to request a new page via user navigation or via javascript. javascript has a couple options to get data to use to update the html.

    1) ajax (xmlhttprequest): javascript can make an async request to the server to get data. it can then convert this data to html and update the dom. ajax is always from javascript, the server can not call the javascript. to do what you want your javascript would poll via ajax checking for updates.

    2) server push. this is an old trick where the server keeps a connection open to an iframe, and pushes javascript to the iframe to be executed.

    3) websockets. this is a new technology for browsers (and windows servers). it allows a persistent peer connection between the browser and the server, that allows two way communication.

    signal/r is a client / server library that implements browser javascript to server peer communication. it uses all three of the above technologies to do this. this would be an appropriate  technology for you to use for the server to fire events to the client. the webhook could send a signal/r event to the browser(s) and the browser javascript could update the page.

    if you wanted to use a view to generate the html, then after receiving the signal/r event, the browser javascript would make an ajax call back to the server (extra network request) with the event data and the server would return a partial view (html), the javascript would then use this html to update the dom.

     

    Friday, June 7, 2019 2:24 PM