How to use SignalR for autorefresh like data - c#

I've been looking into using SignalR for a while now, and I think I have a good candidate for it.
I have a page which allows users of the system to leave Comments, and at the moment it uses JQuery to periodically refresh the list of comments. I think SignalR would replace this nicely, i.e. if there were two users looking at the list and one wrote a comment, I would like it to appear instantly in the second.
All well and good, I have a sort of template where this works.
However
My system itself can sometimes add automatic notifications to the list - These are put into the database directly by a non-web based application.
How can I get SignalR to see the new information from the database and send it to the users?

In SignalR the hub is a static part in your application. You can spin up a System.Threading.Timer in your webapplication to periodically check your database for new notifications and add those to the data used by the hub.
This can even be improved by using a SqlCacheDependency.
A subjective side note: I do agree this type of functionality is a very good candidate for SignalR.

Related

Change notification in MongoDB:using mongo-connector

Linking to this question How to listen for changes to a MongoDB collection? and other similar post here; my requirement is also same: listen to MongoDB for data changes and take some action in my .NET Business layer (probably refresh data ... etc).
searching through different SO post, MongoDB documents, GitHub repository I could find that my best option is to get the required change in OPLOG using Tailable Cursor.
Other that that, I see mongo-connector which can be used to get change notification but I see target system is only solr / ElasticSearch / another MongoDB cluster.
My question: can I use mongo-connector in .NET/C# application to get change notification? Have anyone tried that? it's bit confusing and so thought of posting a question here.
If anyone have already tried, then can you provide a sample code (OR) to some resource which will provide a idea as "How to start...".
I know this is an old question, but I try to share an idea anyway.
Note: I've only started to look into mongo-connector and its capabilities, so my knowledge is a bit limited.
Nevertheless, the mongo-connector has an extension point called DocManager, this entity has basic CRUD methods and provides a way to get notified when your domain entities changes.
So for example you can write your own DocManager. Changes you're interested in (i.e. document update) can trigger specific endpoints in your application. Or if you using some messaging in your application like RabbitMQ you can push those changes to messaging queue and implement different consumers, where each one of them will listen to specific messages.

How to send automatic scheduled emails

There is an ASP.net C# web application through which we can get the recipient emails, time zone and their smtp server details in to the database.I have two requirements:
1. Consider a table in the database. When ever there is a change in the table, an email has to be sent. It is OK if we can constantly check the database every 5 minutes. It would be great if we can send it instantly but a delay is fine.
2. Sending emails automatically at 12 AM at their respective time zone.
I m familiar with C# programming. But kind of new to automatic scheduling stuff. This could sound like a basic question but it would be great if you can help. What is the best way to implement this - Web api or web services or WCF or windows services or combination of web api and task scheduler? Please let me know your thoughts. Also a small tip on how to implement this would be great.
You have an option of setting up trigger but I hate that approach as it will add overhead to your table tow insertion and not actually needed. I think you are in the right path by thinking about pooling. There is a nice little library in .net called hangfire which I find to be very useful to do scheduled task. It has pretty sophisticated reporting and almost all the time works really well. You can give it a try. But if you want to control things better writing a small windows service don't be that bad either. I think doing websevice either using webapi or wcf is a bit overkill here and might not fit purpose.

Using SignalR to Update Client

We have developed a Vehicle Tracking Application in MVC5 using EF6.
The app has a dashboard on which current status of vehicle is displayed i.e. Moving / Stopped/ etc.
To Load current status the dashboard view fires async ajax request(every 20 seconds) to fetch latest data.
Now, rather then making calls from client machine, I want that the client should automatically receive the update as soon as new data is available for its vehicle. It should not poll every 20 seconds.
I've read about SignalR, and tried implementing the Chat program. That's works well. But somehow, I'm unable to figure out "How to use it in my scenario?".
Also, I read about SQLDepedency to detect changes in DB, but again couldn't reach to a solution.
Will be glad, if someone can point me in right direction.
Thanks.
Some time ago I experimented with replacing polling with SignalR too. It was quite straightforward and I used mainly SignalR web as a source of information.
I remember I dealt with some serialization issues but it was more related to the message contract as we used a hierarchy of interfaces and implemented some inheritance in contracts (My question from that time).
Just a suggestion - plan well for scalability - how will your scenario work when you will have to scale out (if applicable) to multiple servers. For my high frequency messaging it was a no-go reason (My question from that time).
maysbe this link can help.
It is SOAP based but also express the fact taht you will have to implement a wbeservice like part on the device.

data driven (real-time) Web frontend

We are looking into a better way to deliver data update notifications to a web front end.
These notifications trigger events that execute business logic and up-date elements via JavaScript (JS) to dynamically update the page without reloading.
Currently this is done with a server side thread, which timely fires an A-synch JS event to notify the web front-end(s) to check if the data has been changed or not.
This mechanism works, but the feeling within the team is that it could be a lot more efficient.
The tool is written in C# / ASP.NET combined with JS and we use the PokeIn library for the aSynch JS/C# Calls.
Any suggestions for improved functionality are welcome! Including radically different approaches still maintaining the JS/C#/ASP.NET usage.
Is this a real question? I would like to add this as a comment but I don't have the enough score.. Anyway, if you need what pokein does for you (object translation among the parties) that is the only option you have. Although there are solutions like websync, signalr.. They don't handle the object translation and has no different approach etc... Better, you benefit from pokein's websocket feature. Both of others needs Windows Server 8 for websocket. Pokein lets you use websocket on any server version or platform..
Sounds like SignalR would help you? This blog post gives a good introduction.
I was trying to solve something similar (reporting real-time updates triggered from an external services communicating with the server) recently and it turned out SignalR is a perfect fit for this situation.
Basically it is a library wrapping long-polling, Web Sockets and few other techniques, using (transparently) whatever is available on server and client.
I only have good experience with it so far.

Which one is better apporach comet or ajax?

i need to update client on any changes occurred on server.
for that i found 2 approach.
1. using ajax which is also known as reverse ajax for this purpose.
2. using COMET.
but i don't know exact difference in both.
my site contains news content and i want that news to be automatically updates when new news is entered by my CMS application.
i have got hundreds of concurrent users on my web application.
please suggest me which approach should i use to get best solution.
also please provide me good example's like for that so that i can implement it.
NOTE: i am using .net framework 2.0 but if its not possible in 2.0 then can also move to 3.5
Thanks.
First start with YAGNI principle. See if it is ok for your client to update periodically, lets say every min or every 30 seconds. Consider things like how much information users can process in this interval. Also take a look at popular news site and how they implement this feature. In this case you are better off using the pull model where your client updates itself by requesting data from server. Implementing a push model is much more complicated.
Once you are sure that you need the push model, IMO comet is a better option. Coment is designed for this purpose. Dojo Foundation's CometD is a widely used library for this purpose. One good example is the live chess application on chess.com
Also, though I am not 100% sure about this, I believe you will need to use technologies like ASP.NET MVC which will allow you more control on the markup generated by your web app so that you can use these libraries.

Categories