How to send updates from server to clients?
Asked Answered
K

4

5

I am building a c#/wpf project. It's architecture is this:

  • A console application which will be on a virtual machine (or my home computer) that will be the server side.
  • A wpf application that will be the client app.

Now my problem is this - I want the server to be able to send changes to the clients. If for example I have a change for client ABC, I want the server to know how to call a service on the clients computer. The problem is, that I don't know how the server will call the clients.

A small example in case I didn't explain it well: The server is on computer 1, and there are two clients, on computers 2 and 3. Client 2 has a Toyota car and client 3 has a BMW car. The server on computer 1 wants to tell client 2 that it has a new car, an Avenger.

How do I keep track and call services on the clients? I thought of saving their ip address (from calling ipconfig from the cmd) in the DB - but isn't that based on the WI-FI/network they are connected to?

Thanks for any help!

Krakow answered 14/9, 2015 at 14:27 Comment(2)
Someone can definitely correct me if I'm wrong, but the clients should be asking if there are updates. This wouldn't exactly be the server's job.Maclaine
What framework are you using for the server/client connection? WCF? Have you considered using something like an Azure Event hub?Cigarillo
B
7

You could try implementing SignalR. It is a great library that uses web sockets to push data to clients.

Edit:

SignalR can help you solve your problem by allowing you to set up Hubs on your console app (server) that WPF application (clients) can connect to. When the clients start up you will register them with a specified Hub. When something changes on the server, you can push from the server Hub to the client. The client will receive the information from the server and allow you to handle it as you see fit.

Rough mockup of some code:

namespace Server{}
    public class YourHub : Hub {
        public void SomeHubMethod(string userName) { 
            //clientMethodToCall is a method in the WPF application that
            //will be called. Client needs to be registered to hub first.
            Clients.User(userName).clientMethodToCall("This is a test.");
        
           //One issue you may face is mapping client connections.
           //There are a couple different ways/methodologies to do this.
           //Just figure what will work best for you.
         }
    }
}

namespace Client{
    public class HubService{          
      
      public IHubProxy CreateHubProxy(){
          var hubConnection = new HubConnection("http://serverAddress:serverPort/");
          IHubProxy yourHubProxy = hubConnection.CreateHubProxy("YourHub");
          return yourHubProxy;
        }
    }

}

Then in your WPF window:

var hubService = new HubService();
var yourHubProxy = hubService.CreateHubProxy();
yourHubProxy.Start().Wait();
yourHubProxy.On("clientMethodToCall", () => DoSometingWithServerData());
Beastings answered 14/9, 2015 at 14:41 Comment(1)
This would be a much more helpful answer if you explained how SignalR could meet this architectural demandCigarillo
C
2

You need to create some kind of subscription model for the clients to the server to handle a Publish-Subscribe channel (see http://www.enterpriseintegrationpatterns.com/patterns/messaging/PublishSubscribeChannel.html). The basic architecture is this:

  1. Client sends a request to the messaging channel to register itself as a subscriber to a certain kind of message/event/etc.
  2. Server sends messages to the channel to be delivered to subscribers to that message.

There are many ways to handle this. You could use some of the Azure services (like Event hub, or Topic) if you don't want to reinvent the wheel here. You could also have your server application track all of these things (updates to IP addresses, updates to subscription interest, making sure that messages don't get sent more than once; taking care of message durability [making sure messages get delivered even if the client is offline when the message gets created]).

Cigarillo answered 14/9, 2015 at 14:43 Comment(0)
D
2

In general, whatever solution you choose is plagued with a common problem - clients hide behind firewalls and have dynamic IP addresses. This makes it difficult (I've heard of technologies claiming to overcome this but haven't seen any in action) for a server to push to a client.

In reality, the client talks and the server listens and response. However, you can use this approach to simulate a push by; 1. polling (the client periodically asks for information) 2. long polling (the client asks for information and the server holds onto the request until information arrives or a timeout occurs) 3. sockets (the client requests server connection that is used for bi-directional communication for a period of time).

Knowing those terms, your next choice is to write your own or use a third-party service (azure, amazon, other) to deliver messages for you. I personally like long polling because it is easy to implement. In my application, I have the following setup.

  • A web API server on Azure with and endpoint that listens for message requests
  • A simple loop inside the server code that checks the database for new messages every 100ms.
  • A client that calls the API, handling the response.

As mentioned, there are many ways to do this. In your particular case, one way would be as follows.

  1. Client A calls server API to listen for message
  2. Server holds onto call, waiting for new message entry in database
  3. Client B calls server API to post new message
  4. Server saves message to database
  5. Server instance from step 2 sees new message
  6. Server returns message to Client A.

Also, the message doesn't have to be stored in a database - it just depends on your needs.

Dungdungan answered 14/9, 2015 at 14:59 Comment(0)
H
0

Sounds like you want to track users à la https://www.simple-talk.com/dotnet/asp.net/tracking-online-users-with-signalr/ , but in a desktop app in the sense of http://www.codeproject.com/Articles/804770/Implementing-SignalR-in-Desktop-Applications or damienbod.wordpress.com/2013/11/20/signalr-a-complete-wpf-client-using-mvvm/ .

Holpen answered 14/9, 2015 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.