MVC.NET: How to update UI on event trigger in backend ? - asp.net-mvc

I want to update the browser UI on the event at controller level, such as some service is updating the database and if database gets updates then, user should get notified about it.
I don't want that browser should keep pinging to server after some seconds.
If you can share some link of working example it'll be helpful.
I read about SignalR but not sure how to use it.
Thanks

options:
1. you can write a javascript script to roll polling a mvc action.
2. you can use SignalR ,to send a message from backend with websocket http://www.asp.net/signalr

Related

Twilio SDK JS, is possible to get status of participant on conference/phone?

I'm trying to get events on specific participants on the client side when they get put on hold. I can set the participant on hold on the backend via REST API, but I wonder how I can get the events of the call or participant when it changes to hold/unhold?
The Twilio Voice JavaScript SDK allows you to make voice calls from the browser, but it will not emit events like when someone is put on hold.
However there are techniques to achieve your goal:
You could create a WebSocket (bidirectional connection) from your web client to your backend, and when you put someone on hold, send an event with the details over the WebSocket. When your web client receives your event, it can update its UI.
You could use Twilio Sync to share and update state in real-time between your web client and your backend. When you put someone on hold, you can update the state on the server, and the web client can receive this update so you can update your UI. There are also other real-time database/services you could use.
You could persist state about your call in your backend and request the data from your web client every couple of seconds. When the data changes, the web client can update its UI. This is not ideal as it sends many unnecessary HTTP requests, but it works.
I recommend the first two solutions, but would avoid the 3rd which I am including for completeness.
This sounds like an interesting project, good luck!

How to dynamically and efficiently pull information from database (notifications) in Rails

I am working in a Rails application and below is the scenario requiring a solution.
I'm doing some time consuming processes in the background using Sidekiq and saves the related information in the database. Now when each of the process gets completed, we would like to show notifications in a separate area saying that the process has been completed.
So, the notifications area really need to pull things from the back-end (This notification area will be available in every page) and show it dynamically. So, I thought Ajax must be an option. But, I don't know how to trigger it for a particular area only. Or is there any other option by which Client can fetch dynamic content from the server efficiently without creating much traffic.
I know it would be a broad topic to say about. But any relevant info would be greatly appreciated. Thanks :)
You're looking at a perpetual connection (either using SSE's or Websockets), something Rails has started to look at with ActionController::Live
Live
You're looking for "live" connectivity:
"Live" functionality works by keeping a connection open
between your app and the server. Rails is an HTTP request-based
framework, meaning it only sends responses to requests. The way to
send live data is to keep the response open (using a perpetual connection), which allows you to send updated data to your page on its
own timescale
The way to do this is to use a front-end method to keep the connection "live", and a back-end stack to serve the updates. The front-end will need either SSE's or a websocket, which you'll connect with use of JS
The SEE's and websockets basically give you access to the server out of the scope of "normal" requests (they use text/event-stream content / mime type)
Recommendation
We use a service called pusher
This basically creates a third-party websocket service, to which you can push updates. Once the service receives the updates, it will send it to any channels which are connected to it. You can split the channels it broadcasts to using the pub/sub pattern
I'd recommend using this service directly (they have a Rails gem) (I'm not affiliated with them), as well as providing a super simple API
Other than that, you should look at the ActionController::Live functionality of Rails
The answer suggested in the comment by #h0lyalg0rithm is an option to go.
However, primitive options are.
Use setinterval in javascript to perform a task every x seconds. Say polling.
Use jQuery or native ajax to poll for information to a controller/action via route and have the controller push data as JSON.
Use document.getElementById or jQuery to update data on the page.

SignalR vs setTimeout

Part of my MVC view page gets refreshed every 30 seconds after fetching some resource from the server. I've been using setTimeOut to trigger a javascript method to fetch data from the server asynchronously, compare it with the old data, and if it has changed, update a div tag. Now, I'm thinking of creating a timer in global.asax class, start it in the application_start event, and in the timer elapsed event, get the data, and send it to all the clients using SignalR only if the data has changed.
Will there be any advantage in using SignalR over setTimeOut here?
The advantage in this case would be you'll avoid an unnecassary trip to the server if that data hasn't changed. Using SignalR, you can broadcast the data to all clients only when the data has changed.
The other advantage, is that SignalR will push from the server to the browser using the best technology available, without you having to worry about it. This could be WebSockets if you're running your server on Windows 8 server with ASP.NET 4.5 (probably a future consideration), or Server Sent Events if the client is Chrome, Firefox or Opera, or Forever Frame if the client is IE. Either way, you don't have to worry about it, SignalR will take care of the transport management for you.
Depending on where your data is stored and how it's updated, you might even be able to do away with the timer completely, and just broadcast the data to all clients immediately whenever it's changed. If it's updated by another action method on a controller, just broadcast to clients from there. If it's updated via some other process directly into the DB, you could setup a SQL query notification in you application (in App_Start) to get alerted when it's changed and then broadcast at that point.

Invoke controller action from a web service

I have an ASP.NET MVC application presenting some data and I want it to
open a new tab, or
redirect to a new view
when a web service gives me a signal. This WS is supposed to handle some external data and requests and when some specific action is called, I want it to be able to give my web application some kind of signal.
I was thinking about using Html.Action to an asynch controller but then I don't know how to provide the signal from the WS to the controller (or all instances of them).
Hopefully, it is understandable. Do you have an idea what needs to be done?
Thanks in advance.
So basically your problem comes down to the fact that server state is changed (through WS) and we want to make something happen at the client side where the your web app is being viewed.
Frankly it's not straightforward. Internet works on the Client -> Server architecture. User sends a request to the server, server responds. What you are trying to do is reverse of it. You want to send a request from server to user. HTTP protocol doesn't work like that.
Right now, to do something similar following two strategies are used:
Websockets : try searching them on google. You create a socket between the client and server and once server gets updated by the WS, it sends a request to the client through the socket. You can ask it to navigate to a different view or open a new window. The downside with it is that its not supported by the majority of the browsers. Might take a year or so to be. Not really recommend now.
Polling : You can make Ajax requests from your browser to server in certain intervals (you know like every 5 seconds) and see if the server state has changed or now. If yes, then do your stuff. That's the most common technique. Twitter.com uses it. There is also another version of it called Comet or Long Polling but I won't confuse you with that.
The important thing to note here is that whatever you want to do (open tab, change view etc) you have to do it through Javascript at the client side.
hope that helps

MVC show notification to user when event happens on server

i've got a situtation where we have a web site (mvc project), and a wcf service, when service gets message i need to show a notification to user who's on the web site with this message contents.
would be glad if you can show me the place where to start!
Thank You !
You probably need some javascript on your page that polls the server at a certain interval to check if there is something to show to the user.
Here is a nice article on how to create a WCF service and call it from the browser using JQuery
I think you need to place javascript timer on your page which will do ajax for example: "/Messages/GetLastUnreadMessage".
You'll need to have a bit of javascript that makes an AJAX call back to your site from the client page on a regular basis that checks to see if there are new messages, and displays them if there are.
There's no way for your web server to 'notify' an already rendered client page in the other direction (providing I understand your requirement correctly).

Resources