4

I need a way for clients (C# applications) of a ASP.NET Web API to be notified of certain changes via the Web API. They don't even need to know what the changes are, just need to get a notification that something changed (at that point it's up to the client to call the API to get any specific data they may need after getting the notification). I'm not sure what a good way of accomplishing this would look like.

I'm thinking one way might be to create events and somehow have the client subscribe to those events, but I don't know how to do that through Web API.

I found some mentions of SignalR on Google, but this seems like a lot of work to implement and seems to do a lot more than I need.

All I need is for the Web API to be able to tell the client "something changed, come and get it". However, I want to avoid polling. What is the fastest/easiest way to do accomplish this?

mayabelle
  • 9,804
  • 9
  • 36
  • 59
  • 2
    SignalR isn't as complicated as you think. The [SignalR](http://www.asp.net/signalr) website has a couple of tutorials which you can repurpose for your own situation. – Brad Rem Dec 31 '13 at 20:18

5 Answers5

7

You really have only two options:

  1. Use SignalR or any kind of Websocket framework.
  2. Have the client apps poll the API to look for changes.

Web API is stateless by design. The API doesn't maintain any kind of connection or state information with any of the client applications. Therefore there's really no way to implement anything like a traditional C# event.

jebar8
  • 2,113
  • 3
  • 21
  • 31
  • Isn't this exactly what I said? :) – Piotr Perak Dec 31 '13 at 19:53
  • @Peri you beat me to the submit button by 30 seconds :) – jebar8 Dec 31 '13 at 19:55
  • Nr 1 in your list above should not say SignalR... It should say SignalrR, SuperWebSocket, Fleck, XSockets.NET etc.. SignalR is not the solution only one of the options. – Kim See Jonge Jan 01 '14 at 10:44
  • @KimSeeJonge I think the downvote was a little harsh. The OP mentioned using SignalR, so that's what I put in my answer. I edited it just for you :) – jebar8 Jan 01 '14 at 17:20
  • Would u mind to update your answer since you put it 2 years ago, please. – NoWar Mar 02 '16 at 17:37
  • 2
    @Dimi as far as I know the answer is still valid. Unless you have further information to add. If so, please edit the answer yourself. – jebar8 Mar 02 '16 at 21:38
5

SignalR would be the coolest way to do it ;)

But the simplest thing to do is just poll server once every X and ask 'Did anything change?'.

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
0

3 years later..and there is one more option that looks very promising which is WebHooks

  1. Polling
  2. SignalR (Server<->Server, Server<->Browser)
  3. WebHooks (Server<->Server)

Difference between SignalR and WebHooks

Community
  • 1
  • 1
A.J.Bauer
  • 2,803
  • 1
  • 26
  • 35
0

I think that SignalR is for bidirectional communication between client and server. For an SSE (Server sent Event) use PushStreamContent instead. https://techblog.dorogin.com/server-sent-event-aspnet-core-a42dc9b9ffa9

Donald
  • 534
  • 1
  • 10
  • 18
0

Google firebase can also be used for this. You can write firebase code in your client application(javascript or mobile app etc.) which can be triggered whenever any change occurs on firebase. So whenever something happens on your server hit the firebase url and that will raise the event which can be caught on the client side.

May be it can help.

vab711
  • 13
  • 4