question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending MediatR INotifications to the client over SignalR

See original GitHub issue

For every INotification you want to send to the client over SignalR, you have to create an INotificationHandler for that specific notification, which then calls the hubcontext to send a specific INotificationMessage (another object alltogether) to the connected clients.

In my app I’m building, I have already quite some notifications like that, and was thinking we could make this probably generic in a way you don’t have to create separate INotificationHandlers and INotificationMessages for all the INotifications you want to be sent to the client as well.

In my research around this, I found this article which is looking very close to what we need: https://remibou.github.io/Realtime-update-with-Blazor-WASM-SignalR-and-MediatR/ There’s an issue there though pointed out in the comments… and another pointer to an interesting library in this regard: https://github.com/KuraiAndras/MediatR.Courier

I think combining those 2 should be able to give a a nice implementation of something like this…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:23 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
fretjecommented, Feb 5, 2022

Oh, an about the INotificationMessage: I’ve updated that interface to just be a marker interface. There were 2 properties in there:

One was string MessageType, which in every implementation returned the value nameof(SpecificNotificationMessage). Which was then only used in INotificationService, to us as methodName parameter in the HubContext.SendAsync calls. There, I simply replaced notification.MessageType with notification.GetType().Name in every call to hubContext.SendAsync, which does exactly the same thing (without having to implement a MessageType string on every INotificationMessage

The other one was just string Message which wasn’t actually needed in every INotificationMessage so no need to be on the Interface. One can easily implement it if it’s actually needed for a specific INotificationMessage.

Maybe this should be in another issue… but I can create a separate PR for this if you want… just let me know!

1reaction
jay-cascadecommented, Jul 28, 2023

Like this:

public class MyEventNotification : IEvent, INotificationMessage
{
}

So something like this should work?

public class HomeCreateNotificationEvent : IEvent, INotificationMessage {
  public string Name { get; set; } = default!;
  public string? Description { get; set; }
  public string Phone { get; set; } = default!;
  public string Colour { get; set; } = default!;
  public string Adr1 { get; set; } = default!;
  public string? Adr2 { get; set; }
  public string? Adr3 { get; set; } = default!;
  public string Town { get; set; } = default!;
  public string? County { get; set; }
  public string PostCode { get; set; } = default!;
}

public class CreateHouseRequestHandler : IRequestHandler<CreateHomeRequest, Guid>
{
    // Add Domain Events automatically by using IRepositoryWithEvents
    private readonly IRepositoryWithEvents<Home> _repository;
    private readonly IEventPublisher _publisher;
    public CreateHouseRequestHandler(
      IRepositoryWithEvents<Home> repository,
      IEventPublisher publisher
    ) => (_repository, _publisher) = (repository, publisher);

    public async Task<Guid> Handle(CreateHomeRequest request, CancellationToken cancellationToken)
    {
        var home = new Home(
          request.Name,
          request.Description,
          request.Phone,
          request.Colour,
          request.Adr1,
          request.Adr2,
          request.Adr3,
          request.Town,
          request.County,
          request.PostCode);

        await _repository.AddAsync(home, cancellationToken);

        await _publisher.PublishAsync(
          new HomeCreateNotificationEvent {
          Name = home.Name,
          Description = home.Description,
          Phone = home.Phone,
          Colour = home.Colour,
          Adr1 = home.Adr1,
          Adr2 = home.Adr2,
          Adr3 = home.Adr3,
          Town = home.Town,
          County = home.County,
          PostCode = home.PostCode
        });

        return home.Id;

    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use SignalR in MediatR Notifications - SchwabenCode.com
In this short code sample I show you how to inform a client about SignalR based on MediatR notifications. First you need its...
Read more >
Realtime Update With Blazor Wasm Signalr And Mediatr
Sending notifications from server to the client. What I want to do is this : when some notifications are sent in MediatR: send...
Read more >
Commands + Domain Events + Real time notification
MediatR was used as an abstraction layer for commands and domain events. SignalR used for sending notifications to the frontend.
Read more >
SignalR Hub sending message but no clients receive it
I have a aspnetcore Mvc website which is running a QuartzNet Scheduler. The Scheduler has listeners which are triggered when a Job is...
Read more >
ASP.NET Core SignalR - Notifications Service - YouTube
ASP.NET Core SignalR real time notifications service using background services, channels and message queues for distributed scenraio.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found