Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send updates from server to clients?

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!

like image 950
user3260639 Avatar asked Dec 25 '22 14:12

user3260639


2 Answers

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:

namepsace 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());
like image 139
ertdiddy Avatar answered Dec 27 '22 21:12

ertdiddy


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]).

like image 39
Dan Field Avatar answered Dec 27 '22 20:12

Dan Field