Implementing SignalR in ASP.NET Core Web App (MVC) for real-time database updates

ChrisU 231 Reputation points
2023-01-11T12:53:12.0533333+00:00

Hi All,

 

I am still a bit green with C# and ASP.NET however I have created a ASP.NET Core Web App (MVC) .NET6, that displays an IOT temperature sensor reading summary dashboard and another page that displays the readings and other information about each individual sensor.  This is not using Entity framework but instead using SQL statements to get the information from the database.  This is going to be developed further, but for now I have been working on making the current information display in real-time.

Currently the temperature displayed for each sensor is queried only when the page loads.  To get the latest, the page needs to be refreshed.  Reading around this I understand that SignalR is the go-to for real-time updates when using ASP.NET.

I have been doing some research and understand the high-level concepts, however, I learn best whilst doing and being able to join it up in my head as I go along (mostly following a tutorial).  Then I can start implementing the functionality for my needs.  The SignalR page (Real-time ASP.NET with SignalR | .NET (microsoft.com)) directs to tutorials that are concerned with chat apps and most online tutorials are also concerned with demonstrating this technology with chat apps.  The few tutorials (written and video) concerned with database real-time updates are either old or not well explained, so I have not been able to either follow them or, when I do complete them, get them to run.

My project basic architecture is below:

User's image

As stated, the queries back to the database occur when the application starts and when the user selects a view. 

Currently both views query one DB table.

The Dashboard Views displays the Dashboard class which contains a COUNT of the total amount of sensors, those that are in Alert and those out of tolerance.  So, three integers are displayed.  The counts for these are queried upon startup and when the dashboard is selected.

The Sensors View displays a box for each Sensor class in a List that is looped through on a Razor page.   Each box contains location information and temperature information for a single sensor.  The temperature information is queried when the sensors view is selected.

In terms of the real-time function I see two ways of doing this.

1)      Polling the database every few minutes to get updates

2)      Updating the frontend only when changes are made to the database

I am not quite sure which is best and whether SignalR can do either, however I suspect that option 2 would be best.

I understand that I would need to add a Hub and JS script to the front end however, I am not entirely sure about how to do this, how it is all connected, and what else I need to do in addition.

Can anyone help?  Can anyone point me in the direction of a tutorial that closely matches what I am trying to do?

Thanks in advance.

 

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.