Advertisement
Advanced .NET MAUI, Networking & APIs

How do you handle real-time updates (WebSockets/SignalR) in a MAUI app?

Short answer

Use the SignalR client (Microsoft.AspNetCore.SignalR.Client) or ClientWebSocket for push updates. Start the connection in a service, subscribe to hub events, and marshal updates to the UI thread.

Save .NET MAUI mock interview
Advertisement
AModel answer
Use the SignalR client (Microsoft.AspNetCore.SignalR.Client) or ClientWebSocket for push updates. Start the connection in a service, subscribe to hub events, and marshal updates to the UI thread. Manage the connection lifecycle with app state—reconnect on resume, stop or background appropriately on sleep.
var hub = new HubConnectionBuilder()
    .WithUrl("https://api.example.com/chat").WithAutomaticReconnect().Build();
hub.On<string>("Receive", msg =>
    MainThread.BeginInvokeOnMainThread(() => Messages.Add(msg)));
await hub.StartAsync();
Real-world example
A chat app receives messages instantly over SignalR and reconnects automatically after transient drops.
Likely follow-ups
  • How do you tie connections to app lifecycle? | What is WithAutomaticReconnect?
Related topics