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.
- Users send a request "Subscribe"
- Server receive a request "Product available!"
- Server sends every user a message with product details.
- 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.
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:
code here – https://github.com/bradwilson/WebstackOfLove
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:
SignalR will invoke the
processItemfunction on the client.Alternatively, you might want to look into JavaScript SSE and Web API
PushStreamContentbut 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/