mayabelle
mayabelle

Reputation: 10014

Web API - Event Notification

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?

Upvotes: 4

Views: 7277

Answers (5)

vab711
vab711

Reputation: 13

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.

Upvotes: 0

Donald
Donald

Reputation: 542

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

Upvotes: 0

A.J.Bauer
A.J.Bauer

Reputation: 3001

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

Upvotes: 0

jebar8
jebar8

Reputation: 2133

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.

Upvotes: 7

Piotr Perak
Piotr Perak

Reputation: 11088

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?'.

Upvotes: 5

Related Questions