Simple Blazor server app event service that handles real-time events for multiple users
// register service
Services.AddAppEventService<TodoUpdatedEvent>();
// inject to component
@inject IAppEventService<TodoUpdatedEvent> _appEventService;
// create dispatcher
var todo = ...;
_dispatcher = _appEventService.GetOrCreateDispatcher(todo.Id);
// create action
public void OnTodoUpdated(TodoUpdatedEvent ev)
{
InvokeAsync(() =>
{
// do something with event
...
StateHasChanged();
});
}
...
_dispatcher.AddAction(OnTodoUpdated);
...
// publish event
await _dispatcher.PublishEventAsync(new TodoUpdatedEvent());
// cleanup
_dispatcher.RemoveAction(OnTodoUpdated);
_appEventService.DestroyDispatcher(_dispatcher.Key);NuGet\Install-Package AppEventService -Version 1.0.0.1