C
C#3w ago
Mawral

Difficulty with Blazor Server chat app

Hi, I'm trying to build a straightforward group chat application with Entity Framework and Blazor Server, but I'm totally new to the latter and struggling to make it do what I want. I currently have the EF models set up, and a set of components including a 'ChatLog' for displaying messages, and a 'InputForm' for users to create new messages. The 'InputForm' can register a sent message and save it in the database, which will appear in the 'ChatLog' when the page is refreshed. But I'm clueless on how to make the 'ChatLog' update automatically with sent messages, and/or how I would use a SignalR Hub to make all other connected users load the new chat messages too. Could someone point me in the right direction?
4 Replies
SleepWellPupper
If you're using blazor server and don't do fancy stuff like hosting multiple instances of your app then you don't need a hub All clients on blazor server have access to the server application, so they can access the same memory. You could share messages between them by implementing some container for your messages, have new messages be posted to that and also have it raise an event upon receiving a new message. Then use the same instance for all clients that need to communicate, wire the event to InvokeAsync(StateHasChanged) on the chatlog component. Sparse detail, I know. But the gist is your clients already share a memory space so just share information in-process instead of using a signalr hub
Joschi
Joschi3w ago
Iirc. singleton services in blazor server share state between connections.
SleepWellPupper
Yes, singleton services, static instances etc.
Mawral
Mawral3w ago
Much thanks to you both - I'll try to set up a container for message logs and see how that goes.