Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback methods in ASP.NET Web API

I have an ASP.NET Web API server, that have to communicate with different applications on different platforms. And now I want to create a method that would be something like a callback: client application subscribes to it and waits until server fires a message.

Example: Many users are waiting until new product will be available in store - they subscribe to this "event". When product arrives in store - every customer receives a message, which have to be handled in some case.

  1. Users send a request "Subscribe"
  2. Server receive a request "Product available!"
  3. Server sends every user a message with product details.
  4. User's application processes the message

I tried to find some information about callbacks or duplex in ASP.NET Web API, but the one advice - it's better to use WCF for this approach.

Solutions

  • In every client application create something like timer, that every N seconds sends a request "Is product available?" until gets "false". When the response will be true - send a message "Get product details". It's causes a lot of traffic and if there will be many clients with these timers - it would be something bad, isn't it?
  • Create a small callbacks-server (maybe WCF). But in this case would be a lot of problems with communication between this server and apps on different platforms.
  • Maybe there are some solution in ASP.NET Web API, that i missed.

If you have some ideas how i can solve this problem, please give me an advice.

Thanks for help.

like image 992
Ihor Kliushnikov Avatar asked Jan 06 '13 17:01

Ihor Kliushnikov


People also ask

What are callback methods in C#?

A "callback" is a term that refers to a coding design pattern. In this design pattern executable code is passed as an argument to other code and it is expected to call back at some time. This callback can be synchronous or asynchronous.

What is callback function in asp net?

In a client callback, a client script function sends a request to the ASP.NET Web page, which then runs an abbreviated version of its normal life cycle to process the callback. To ensure that callback events originate from the expected user interface (UI), you can validate callbacks.

What are callbacks in API?

A callback is an asynchronous API request that originates from the API server and is sent to the client in response to an earlier request sent by that client. APIs can use callbacks to signal an event of interest and share data related to that event.

What is difference between callback and postback?

think of it as 'sending the server the whole page (asp.net) full of data'. On the other hand, a callback is also a special kind of postback, but it is just a quick round-trip to the server to get a small set of data (normally), and thus the page is not refreshed, unlike with the postback...


1 Answers

Looks like you want push notifications from your server - which, in this case, can be achieved by combining SignalR with Web API.

Brad Wilson has a great example on this:

  1. code here - https://github.com/bradwilson/WebstackOfLove

  2. NDC Oslo talk explaining all this - http://vimeo.com/43603472

In short, whenever you add new item to the Web API, you can notify all the connected (subscribed) clients:

public void PostNewItem(ToDoItem item)
        {
            lock (db)
            {
                // Add item to the database
                db.Add(item);

                // Notify the connected clients
                Hub.Clients.processItem(item);
            }
        }

SignalR will invoke the processItem function on the client.

Alternatively, you might want to look into JavaScript SSE and Web API PushStreamContent but that is much more low level and SignalR abstracts a lot of this type of stuff for you so it might be more complicated to deal with.

I blogged about this approach here http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/

like image 118
Filip W Avatar answered Oct 25 '22 17:10

Filip W