I have a Windwos Application (Let's name it App) and a WebService Project (name it WS) and a SqlServer Database (DB), and the technologies are all from Microsoft and .net.
The roles are that whenever App needs to do an action, it calls WS and WS does the magic work with DB and then returns the result to App.
So far, so good, but I need something more than that. I need a third Application, let's call it a Robot, this Robot monster should have the ability to find all alive clients (App instances) and not kill, but call them on some specific times, then the App(s) will decide do an action on being called.
My information lacks here, and that is why I want you guys to help me find the best solution for this Server-Calls-Client-And-Client-Does-Something thing.
I have very short handed and pragmatic solution ideas:
Each client application invokes a method for instance YesIamAlive() of the webservice each x seconds/minutes. If the server gets this request it will be saved so you are be able so see which clients are alive. Each client which not sending an alive request for the last x seconds / minutes is not any longer alive. Another method which is also called on a routinely basis and it forces the client to do an action.
You could use SignalR for a websocket communication between your server and client. This example shows a chat server, which is not simular to your request but it shows the idea behind it:
http://braindrivendevelopment.com/2013/01/28/signalr-with-windows-azure-cloud-services/
I am quite sure that there are even more elegant solutions for your problem.
SignalR (GitHub) is an excellent framework for "pushing" to clients in near real-time. It works with both web and WinForms clients.
Related
1- Client application sends a a request to an http server (ashx file, IHttpHandler).
Remark-1: Client is a .dll which will be hosted by other stand alone applications.
Remark-2: Server was first developed as a web service, then for unknown reasons it became very slow, so we implemented it from scratch.
2- Server registers the request in database so that a long duration process is performed on data.
3- Client needs to get notified when the process is finished.
First thing that crossed my mind was implementing a Timer in client. Although I'm not sure if it is ok to do it inside of a host application which is not aware of such usage.
Then it crossed my mind if there may be a something useful in TcpListener or lower layers of socket programming instead of a high frequency timer and flooding server with update requests.
So, I appreciate any suggestion on proper way of doing this task.
UPDATE:
After giving some order to my codes, I update requirements like this:
1- Server "Broadcast"s ID of clients, like: "Client-a, read your instructions", then "Client-c", "Clinet-j",... . This is not mission critical, if a client looses the broadcast, it will return after one minute by a timer tick and will check instructiosn.
2- This server is gona be hosted on a shared hosting plan, at least at first. Solution must be acceptable in boundary of share hosting.
3- preferably all clients connect to the only one socket. No usage of extra resources.
Any recommendation is appreciated.
I have a chat SignalR server, the chat support group chatting.
I also have a server which actually creates the groups and other group managment tools.
Whenever a user leaves a group (via http post ) to server, I want the chat service to trigger some methods, such as LeaveGroup and some other logictics.
I bound the connectionId to userId so I got the parsing request covered.
QUESTION IS: What is the best practice of communication between server/service and the signalr server.
Taking in mind, I dont want to compormise on scalability on each of my servers/services.
My idea is more or less host a web api server inside the SignalR server, but I can't seem to find any topics suggesting that could damage the performance.
Ideas?
Thanks alot.
p.s
I know that there is no code involved in here. but it seems irrelevant. I have self hosted web api in a window service I have, so the code is pretty much the same.
I would love to provide more data/information if thats neccesary
It seems like this documentation is most applicable to what you're trying to do: https://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr
It speaks specifically about how to communicate from your server/service application to the signalr clients. Communicating from the client to the server/service could be done either through the signalr hub, or with other web API.
From a best practice perspective, the documentation specifically states (https://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub):
If you need to use the context multiple-times in a long-lived object,
get the reference once and save it rather than getting it again each
time. Getting the context once ensures that SignalR sends messages to
clients in the same sequence in which your Hub methods make client
method invocations. For a tutorial that shows how to use the SignalR
context for a Hub, see Server Broadcast with ASP.NET SignalR.
If you're really into scalability, you might want to look into integrating your signalr communications into some other message queueing system, but that's probably overkill for most circumstances.
I'm using EF (6) with ASP.net MVC (c#). My hardware architecture:
main server - include windows service.
web-server - running my web-application UI.
The users request for some answer. My web-site adds the question to the Data-Base (SQL - server) with EF.
In my main server, the service detects the changes (new task arrived) and solve it.
The detection of new task made by busy waiting, which I do not like.
How it's being done:
My EF layer includes custom function, which fire every N minutes SQL stored procedure.
If the SP retrieve information, the service solve it.
What I want:
when new task added from the web-site, the web-site will send signal to the main server. When the signal arrives to the main server, it will immediately start to solve it.
How this can be done?
Thank you!
seems like a perfect use case for singalR....
please take a look at http://www.asp.net/signalr
signalR will choose the best suited communication method. it also uses sockets if available otherwise fallback to other means of transport.
You can create one more service for Comm. between two servers .
Solution implemented with WCF is very trivial to Develop ..
Whenever new Question is posted .. your webserver can invoke this webservice ..
bottom - line : Look into WCF
We do this a lot, I think you have a few options:
A shared storage mechanism which is polled from the receiver (what you are currently doing)
Using something like a webservice call to send a command to the receiver (Normally a synchronous processing technique)
Using a messaging framework such as NServicebus to send the command to the receiver (an asynchronous fault tolerant technique)
We use all of these techniques, but the one I normally find best is the last one.
Messaging introduces fault tolerance and asynchronous processing which are both very useful when building systems spanning multiple machines.
If this sounds like something that might help check out the pubsub example for NServiceBus here: http://support.nservicebus.com/customer/portal/articles/860297-how-pub-sub-works
Or if fault tolerance doesn't really matter to you I would recommend a selfhosted webapi running inside your windows service.
I am looking for a way user can communicate between an ASP and Winform applications.
I am looking for something like soluto.com, I want to let the user send commands to other computers via Website. So let's say the user signed up for 10 computers, which is registered on the mvc app. User can select all the 10 computer and send a "Do this task" with a click of a button.
I am thinking something like, Winform will create a httplisten server. Everytime winform is open, it will send a "I am online" post to mvc, along with IP:Port. The server will send a request to that ip:port when required.
That approach seems very unsecure though, having an open port, configuring firewall and etc, seems like a overkill.
I was wondering if there way any other way of accomplishing this.
Thank you for the help.
P.S. Before you claim this is a stupid idea, Piriform is doing something like this also. Take a look at Agomo.com
Use SingalR with properly architected web and windows applications (e.g. MVP, MVC, etc.)
SignalR with window client (WPF)
Console App & SignalR
Create a WCF service within the WinForm application, specify endpoint(s) (and secure the endpoint appropriately), and connect to said endpoints from your ASP.NET application the same way you would also connect to a WCF service.
Why don't you just have the Winforms app use a standard HttpClient or WebRequest to periodically poll the service (maybe every 5 seconds or so) and ask if there if there are any tasks that need to be performed?
Unless you need realtime, low-latency, high performance communication then this is the easiest way to solve your problem with minimal to zero client side setup or security configuration.
The way I would do it is implement it like a stack in a data persistence layer. So each client could have rows in a table that are added when a task is queued. When the clients sends an HTTP GET request to the MVC server it will return the an array of tasks for that client and you could have it either delete them from the database right away or wait for the client to send a HTTP command later to indicate which tasks it completed.
You could represent tasks as a simple data object with a few properties, or just a string or int that you can lookup on the client in some way to invoke the appropriate code.
For reasonable security each client just needs to be given a unique key like a GUID or equivalent that it can later send to the server to validate its identity. This is also known as a cookie, secret, or API key.
I'm trying to use WCF to implement a comet style server push to an ajax web application.
In my WCF service, I've implemented a WaitForEvents method that calls Monitor.Wait to suspend the thread until new data arrives. At that point, the Monitor is pulsed, and the method returns the new data which closes the comet style request.
The request is made again when this happens.
Currently, this works fine but I noticed that WCF needs to create a new thread for each connected user. This is probably because the thread cannot be returned to the threadpool until data comes in, and so each connected user requires a new thread.
I want to make this implementation more efficient by having one thread service multiple connections. If I were to use a socket, this could be done by leaving the socket open and returning the thread to the thread pool first. When new data arrives, it will be delivered by another thread and we can write the new data directly to the socket and close it.
Does anybody know how this can be done via WCF?
I have been looking at "Push-Style Streaming" http://msdn.microsoft.com/en-us/library/bb472551.aspx and they mention that "WCF implements a "pull" model in which the application code (the service) returns an instance of Stream and relies on the lower-level infrastructure to pull data from this stream and write it out to the network." but I cant find any examples of this one the website.
Many thanks in advance!
Check out nComet
It's not using WCF, but I believe the author is working on a version that uses WCF. Contact him via codeplex and ask him :-)
"nComet is a .NET implementation of
the Comet (reverse-AJAX push)
architecture. This server-side
pipeline uses long-lived
client-initiated HTTP connections to
push messages to the client. Once the
client receives a response, it
immediately opens another HTTP
request, which the server holds until
a message is ready. This architecture
allows the server to push dynamic
html/xml/json/etc to the browser,
rather than the browser polling the
server.
This project is scoped to the .NET
server-side architecture, initially
providing a HttpListener (for a custom
host communicating with HTTP.SYS
directly) as well as a ASP.NET
implementation, where the ASP.NET
implementation can be hosted inside
IIS as well as an external process.
The library will simplify the
implementation of common message
patterns such as pushing the latest
data, as well as sync. Example code
and links to multiple client-side
javascript implementations will also
be provided."
You may also want to check out WebSync, a .NET comet implementation. Works just dandy with WCF.
(Disclaimer: I work for the company).