Custom Webhook Receiver listening to an Azure Web Job notifications

771 views Asked by At

Is it possible to create a Webhook receiver which listens to a Azure Web Job notification whenever some record gets inserted in database (monitored by Web Job)? Once the notification is received by the Web hook, the UI needs to present a notification to the user.

1

There are 1 answers

0
PerfectlyPanda On

Since Sumit Saini has not posted their code, I'll add the code I wrote for a conference presentation using .Net Core. The original code received events from Event Grid, but it is easily modified to use a simple webhook.

The controller defines an endpoint to receive the event and passes it along to SignalR:

[ApiController]
[Produces("application/json")]
public class EventHandlerController : Controller
{
    private readonly IHubContext<NotificationHub> _hubContext;

    public EventGridEventHandlerController(IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }

    [HttpPost]
    [Route("api/EventHandler")]
    public IActionResult Post([FromBody]object request)
    {

        object[] args = { request };

        _hubContext.Clients.All.SendCoreAsync("SendMessage", args);

        Console.WriteLine(truck);

        return Ok();
    }
}

There isn't any special code in the hub class-- you just define it as normal:

public class NotificationHub : Hub
{
    public async Task SendMessage(Message message)
    {
        await Clients.All.SendAsync("Notification", message);
    }
}

The client is also a standard SignalR implementation.

You can find a more generic SignalR tutorial here: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.2&tabs=visual-studio

You can also use SignalR as a service or Notification Hub to achieve this result.