C# API – Let Xamarin Clients Listen When Server Sends an API

apiasp.net-mvccclient-serverxamarin

My situation

I will make a Xamarin application for Android that can access an ASP.NET MVC webserver with API's. The problem is when I'll send an API to the server, all the connected clients must receive an API with the changes that happens by client one. An example below:

In other words: The server must broadcast data by an API to all the clients when the they must update the UI with the information he get from the server.


The problem

The problem is on the client. How knows the clients when the server send an API to the client?


My idea

I had an idea to solve this problem with an infinitive loop on a thread (otherwise the app will block of course). The problem is now when there happens nothing on the server, there aren't send API's to the clients. But they are always listening to the server when there is nothing to do for a long time. Here is some pseudo code:


Are there better ideas then mine?

I'm wondering if there is an other secure solution for do the same things. I will something like open the connection and let the client wait if there is something to do without using an infinitive loop, like pseudo code below. If the client goes away, the connection to the server close.

Can you suggest me a good hint or pseudo code how I could do this?

P.S.: I make my applications with C#.

Best Answer

Check out SignalR. It enables long running web connections that let you send events from server to client.

If you're making an app, maybe you could use Push for the events?

Or you could even make a simple loop that checks for updates. Kind of like your idea, but separate the check from the data fetching for performance.

Yet another way is to use some kind of socket instead of ordinary http. Since you build an app you are not limited to web techniques.

Related Topic