Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created July 6, 2023 18:33
Show Gist options
  • Save dcomartin/a86eabd6a9c033129ec6ae85f6c10ace to your computer and use it in GitHub Desktop.
Save dcomartin/a86eabd6a9c033129ec6ae85f6c10ace to your computer and use it in GitHub Desktop.
using BlazorClient;
using Microsoft.AspNetCore.SignalR;
namespace UndoExample;
public record Tweet(Guid TweetId, string Message) : ICommand;
public record Tweeted(Guid TweetId, string Message) : IEvent;
public class TweetHandler : IHandleMessages<Tweet>
{
private readonly ILogger<TweetHandler> _logger;
public TweetHandler(ILogger<TweetHandler> logger)
{
_logger = logger;
}
public async Task Handle(Tweet message, IMessageHandlerContext context)
{
_logger.LogInformation($"Tweeted: {message.Message}");
// Do something here like persisting the tweet to the DB.
await context.Publish(new Tweeted(message.TweetId, message.Message));
}
}
public class TweetedSignalRHandler : IHandleMessages<Tweeted>
{
private readonly IHubContext<TweetHub> _hubContext;
public TweetedSignalRHandler(IHubContext<TweetHub> hubContext)
{
_hubContext = hubContext;
}
public async Task Handle(Tweeted message, IMessageHandlerContext context)
{
await _hubContext.Clients.All.SendAsync("tweeted", message.TweetId, message.Message, context.CancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment