Signal-R, with or without Web API? [duplicate] - asp.net

This question already has answers here:
Can I incorporate both SignalR and a RESTful API?
(3 answers)
Closed 9 years ago.
I like how Web API allows to create a RESTful interface. Also I like how Signal-R allow you to push updates to clients.
But is OK to use both at the same time? I mean, Signal-R keeps a connection to the server all the time (or simulates that with long polling), is not a waste of resources to continue creating different connection to perform interactions in Web API?
I could interact with the server through the Signal-R connection, but then I loose all the great Web API functionality.
Cheers.

I've seen this question pop up repeatedly and my answer has always been that you should use SignalR only for real-time messaging (e.g. either notifications from the server or inter-client messaging) and use WebAPI everywhere else (e.g. client just telling the server to do something/asking for some resource).
While you could use SignalR for the latter, once you get to a certain scale you are incurring a massive penalty with respect to how each message to the server has to be fanned out across a backplane implementation just so that eventually only the server actually needs to acknowledge it.

Related

How to push data to multiple clients

I need to build a server to an application who's client side is written in Lua using Corona SDK (application for mobile smartphones and tablets), and server side in .NET.
I have thousands of clients who I need to push data to in a form of XML file or JSON array, every few hours or daily. (Not notifications. Data that needs to reach the application directly and trigger certain actions).
Is there a way to push the data without WCF?
The only other way I found was SignalR, but my client isn't a web browser so I ruled it out.
In my searches I found in stackoverflow a question with a recommendation to push data using 0MQ (http://zeromq.org/), I just found it hard to understand if it fits my needs, can I queue so many clients in one server and delay the answer a few hours or days?
Other recommendations would be greatly appreciated, thank you.

How to Design a Database Monitoring Application

I'm designing a database monitoring application. Basically, the database will be hosted in the cloud and record-level access to it will be provided via custom written clients for Windows, iOS, Android etc. The basic scenario can be implemented via web services (ASP.NET WebAPI). For example, the client will make a GET request to the web service to fetch an entry. However, one of the requirements is that the client should automatically refresh UI, in case another user (using a different instance of the client) updates the same record AND the auto-refresh needs to happen under a second of record being updated - so that info is always up-to-date.
Polling could be an option but the active clients could number in hundreds of thousands, so I'm looking for a more robust and lightweight (on server) solution. I'm versed in .NET and C++/Windows and I could roll-out a complete solution in C++/Windows using IO Completion Ports but feel like that would be an overkill and require too much development time. Looked into ASP.NET WebAPI but not being able to send out notifications is its limitation. Are there any frameworks/technologies in Windows ecosystem that can address this scenario and scale easily as well? Any good options outside windows ecosystem e.g. node.js?
You did not specify a database that can be used so if you are able to use MSSQL Server, you may want to lookup SQL Dependency feature. IF configured and used correctly, you will be notified if there are any changes in the database.
Pair this with SignalR or any real-time front-end framework of your choice and you'll have real-time updates as you described.
One catch though is that SQL Dependency only tells you that something changed. Whatever it was, you are responsible to track which record it is. That adds an extra layer of difficulty but is much better than polling.
You may want to search through the sqldependency tag here at SO to go from here to where you want your app to be.
My first thought was to have webservice call that "stays alive" or the html5 protocol called WebSockets. You can maintain lots of connections but hundreds of thousands seems too large. Therefore the webservice needs to have a way to contact the clients with stateless connections. So build a webservice in the client that the webservices server can communicate with. This may be an issue due to firewall issues.
If firewalls are not an issue then you may not need a webservice in the client. You can instead implement a server socket on the client.
For mobile clients, if implementing a server socket is not a possibility then use push notifications. Perhaps look at https://stackoverflow.com/a/6676586/4350148 for a similar issue.
Finally you may want to consider a content delivery network.
One last point is that hopefully you don't need to contact all 100000 users within 1 second. I am assuming that with so many users you have quite a few servers.
Take a look at Maximum concurrent Socket.IO connections regarding the max number of open websocket connections;
Also consider whether your estimate of on the order of 100000 of simultaneous users is accurate.

Is it possible to implement Database backed Push Notifications in ASP.NET WebAPI

I'm working on a client and server application, in which client requests 'client-specific' data from the server. I'm planning on switching to ASP.NET WebAPI for server so that client can take advantage of available .NET APIs to query server for data.
This scenario works perfectly fine when the client initially connects and requests data from server, However, instead of client constantly polling for data, it should just establish a persistent connection and the server should be able to monitor the database for changes and push new data to the client that has stale data. I came across SignalR and found that it can be used with WebAPI, but can't figure out how to integrate it with database monitoring i.e. a thread or process that is constantly monitoring database for updates. Any solution? I'm open to other non-WebAPI based .NET technologies as well - basically anything .NET based that will cut-down on the development time.
ASP.NET Web API supports PushStreamContent. Take a look at the section "Push Content" in http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx. Here, a timer triggers content written into the stream but you can use that to poll database and write into response or build some other better mechanism
signalR could fit your requirement. You can broadcast a message to all your user or just to part of them. Your client could be a browser (with javascript) or a .net client (WPF or else).
From the server, you can call a method on the client just like that
Clients.All.MyMethodOnTheClient(param1, param2)
or
Clients.Client(connectionId).MyMethodOnTheClient(param1, param2)
The topic is too broad to answer it in 5 min. I can advise you to start here.
Then browse the server api guide, the javascript guide and the .net client guide.
Don't worry. It's quite fast to have a complete tour.

Twitter REST API vs Streaming API - how does it impacts server side

There is an application that should read user tweets for every registered user, process them and store data for future usage.
It can reach Twitter 2 ways: either REST API (poll twitter every x mins), or use its Streaming API to get tweets delivered.
Besides completely different implementations on server side I wonder what are other impacts on server side?
Say application has tousands of users. Is it better to build kind of queue and poll twitter for each user (the simplest scenario), or is it better to use Streaming API and keep HTTP connection open for each user? I'm a bit worried about the latter as it'd require keeping tousands of connections open all the time. Are there any drawbacks of that I'm not aware of? If I'd like to deploy my app on Heroku or on EC2 instance, would it be ok or are there any limits?
How it is done in other apps that constantly need getting data for each user?

Architecture For A Real-Time Data Feed And Website

I have been given access to a real time data feed which provides location information, and I would like to build a website around this, but I am a little unsure on what architecture to use to achieve my needs.
Unfortunately the feed I have access to will only allow a single connection per IP address, therefore building a website that talks directly to the feed is out - as each user would generate a new request, which would be rejected. It would also be desirable to perform some pre-processing on the data, so I guess I will need some kind of back end which retrieves the data, processes it, then makes it available to a website.
From a front end connection perspective, web services sounds like it may work, but would this also create multiple connections to the feed for each user? I would also like the back end connection to be persistent, so that data is retrieved and processed even when the site is not being visited, I believe IIS will recycle web services and websites when they are idle?
I would like to keep the design fairly flexible - in future I will be adding some mobile clients, so the API needs to support remote connections.
The simple solution would have been to log all the processed data to a database, which could then be picked up by the website, but this loses the real-time aspect of the data. Ideally I would be looking to push the data to the website every time the data changes or now data is received.
What is the best way of achieving this, and what technologies are there out there that may assist here? Comet architecture sounds close to what I need, but that would require building a back end that can handle multiple web based queries at once, which seems like quite a task.
Ideally I would be looking for a C# / ASP.NET based solution with Javascript client side, although I guess this question is more based on architecture and concepts than technological implementations of these.
Thanks in advance for all advice!
Realtime Data Consumer
The simplest solution would seem to be having one component that is dedicated to reading the realtime feed. It could then publish the received data on to a queue (or multiple queues) for consumption by other components within your architecture.
This component (A) would be a standalone process, maybe a service.
Queue consumers
The queue(s) can be read by:
a component (B) dedicated to persisting data for future retrieval or querying. If the amount of data is large you could add more components that read from the persistence queue.
a component (C) that publishes the data directly to any connected subscribers. It could also do some processing, but if you are looking at doing large amounts of processing you may need multiple components that perform this task.
Realtime web technology components (D)
If you are using a .NET stack then it seems like SignalR is getting the most traction. You could also look at XSockets (there are more options in my realtime web tech guide. Just search for '.NET'.
You'll want to use signalR to manage subscriptions and then to publish messages to registered client (PubSub - this SO post seems relevant, maybe you can ask for a bit more info).
You could also look at offloading the PubSub component to a hosted service such as Pusher, who I work for. This will handle managing subscriptions and component C would just need to publish data to an appropriate channel. There are other options all listed in the realtime web tech guide.
All these components come with a JavaScript library.
Summary
Components:
A - .NET service - that publishes info to queue(s)
Queues - MSMQ, NServiceBus etc.
B - Could also be a simple .NET service that reads a queue.
C - this really depends on D since some realtime web technologies will be able to directly integrate. But it could also just be a simple .NET service that reads a queue.
D - Realtime web technology that offers a simple way of routing information to subscribers (PubSub).
If you provide any more info I'll update my answer.
A good solution to this would be something like http://rubyeventmachine.com/ or http://nodejs.org/ . It's not asp.net, but it can easily solve the issue of distributing real time data to other users. Since user connections, subscriptions and broadcasting to channels are built in to each, that will make coding the rest super simple. Your clients would just connect over standard tcp.
If you needed clients to poll for updates then you would need a que system to store info for the next request. That could be a simple array, or a more complicated que system depending on your requirements and number of users.
There may be solutions for .net that I am not aware of that do the same thing, but those are the 2 I know of.

Resources