2

So now that Angular 2 is in beta I have been using observables on the client side and I have been using ASP.NET Core Web API on the server side. My question is if I want to display a value on my website as that value changes I understand how to do it on the client side with Observables, but how do I use Observables with a Web API service on the server?

Thanks for your time....

Irv Lennert
  • 555
  • 5
  • 20
  • 1
    You need to show your code. – Enigmativity Feb 17 '16 at 00:55
  • You would want to look at a Push technology from the webserver (WebSockets, SignalR, CrossBar.io) other wise you would just be polling the server (HTTP). If you do choose to poll until you can upgrade to push, you can wrap that to look like an Observable sequence. – Lee Campbell Feb 19 '16 at 02:23

1 Answers1

1

If I understand your question correcly, you can use Refit , to generate the implementation of an interface that returns an IObservable from a REST API.

For the example below, an interface is declared for the /users/{user} REST API. The return type is an IObservable and when you compile this, Refit generates the implementation.

public interface IRestAPI
{ 
    // Returns the raw response, as an IObservable that can be used with the
    // Reactive Extensions
    [Get("/users/{user}")]
    IObservable<HttpResponseMessage> GetUser(string user);
}

To initialize it, you do something like this:

 var api = RestService.For<IRestAPI>("http://api.example.com/users"); 
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61