Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.46 KB

File metadata and controls

52 lines (42 loc) · 1.46 KB

Blazor + SignalR + pgNotifyNet + PostgresSQL notify/listen sample application

build

An implementation of real time notification using SignalR and the PostgreSQL listen/notify feature + PgNotifyNet

Alt Text

docker-compose up
http://localhost:5111

Listen to database changes:

builder.Services.AddPgNotifyNet(builder.Configuration.GetConnectionString("SampleDatabase"),
                    c => 
                        c.Trigger(t => 
                            t.OnTable<Category>(table: "categories", schema: "public").After(Change.Update, Change.Insert)));

Handle notification from database and publish to clients via SignalR:

public class CategoryNotification : IHandleNotification<Category>
{
    private readonly IHubContext<DbNotificationHub> _hubContext;

    public CategoryNotification(IHubContext<DbNotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }

    public Task OnDataChanged(Category oldData, Category newData, Change change)
    {
        _hubContext.Clients.All.SendAsync("onCategoryUpdated", oldData, newData, change);
        return Task.CompletedTask;
    }
}

Update client

@code{
        hubConnection.On<Category,Category, Change>("onCategoryUpdated", (oldCategory,newCategory,change) =>
        {
            ...
        });
    }