C#C
C#3y ago
xdd

✅ Postman / SignalR

hi, so i have hub
public class ChatHub : Hub
{
    private readonly IApplicationDbContext _db;
    public ChatHub(IApplicationDbContext context)
    {
        _db = context;
    }

    public override async Task OnConnectedAsync()
    {
        await Clients.Caller.SendAsync("loadMessageHistory", _db.Messages.ToList());
        await base.OnConnectedAsync();
    }
}

is it possible to send data with first connection request?
for do like...
int chatId = // Getting id from body
var chat = _db.Chats.Inclued(c => c.Messages).FirstOrDefault(c => c.Id == chatId);
// var messages = _db.Messages.Where(m => m.ChatId == chatId).ToList();
if(messages != null)
{ 
  await Clients.Caller.SendAsync("loadMessageHistory", chat/messages);
  await base.OnConnectedAsync();
}
else
{
  // Bad connection or smthing like this
}

or maybe there is some conventional way to do that
image.png
Was this page helpful?