Real time notification in ASP.NET core mvc

I would like to ask question about possibility of having real time notification in asp.net core project. Unfortunately SignalR doesn't seem as an option in this case.

So I have project with EF and SQL server and razor pages. In this project there are two groups admins and normal people. Normal people have access to submit form with simple fields and I would like to every time user submits a form modify layout so admin would see some red ! to inform him there is new form to review.

I have tried to call method which would check it from view but I didn't succeed. Also I¨ve been checking SignalR library but this is working with javascript on click listener and this cannot be attached to method responsible for submitting forms.

Anyone know how this could theoretically work?

Upvotes: 1

Views: 5635

Answers (1)

poke
poke

Reputation: 387765

Usually, everything on the web is bound to a fixed request/response pattern. So you request something from a server, and it responds with something and then the connection is closed. This makes it very difficult to have some kind of communication channel where you are actively notified of server-side changes.

Nowadays there are generally two different methods for this:

  • Polling: This generally means that the client is continuously asking for an update. So every X seconds, a new request will be made to the server to ask for changes.

    There is also an alternative called “long polling” in which the connection is kept open for longer. The client makes a normal request but the server takes it time to respond, keeping the response available until an actual update has occurred (in which case the response is then sent).

  • Push: This can use various technologies to have the server actively push information to the client. There are some proprietary solutions for mobile devices, but there’s also the standardized Push API which however isn’t currently well supported in browsers.

    Another common way to use Push is to simply have some permanent connection. This is commonly done with WebSockets which is basically a full-duplex channel that is established between the client and the server, allowing both to send data at any moment.

For real time applications, WebSockets are usually the way to go, although depending on your clients, you may need to have some fallback in form of polling. SignalR does exactly that: It provides you with a high-level API for the client to interact with the server and the server to interact with the client, without you having to worry about how the underlying connection works.

Also I’ve been checking SignalR library but this is working with javascript on click listener and this cannot be attached to method responsible for submitting forms.

SignalR uses JavaScript, yes, but any solution will require JavaScript here since you are asking for interactivity on the client side. So you need to establish a SignalR connection at the beginning of your web page, and then you can actually publish messages from the server to connected clients when a request is being made by someone else.

So the requirement, to push changes made by others down to your admins, can be satisfied by SignalR if done properly.

Upvotes: 4

Related Questions