Search code examples
.netmauiasp.net-core-signalr

When and how to create and start hub connection in an MVVM way. .NET MAUI SignalR


I wish to create and start a hub connection to my server when I navigate to a certain page, but I'm unsure of the best way to do this in a somewhat MVVM way. I was initially going to set up and start the hub connection in the view model constructor but should I instead override a method in the code behind such as OnAppearing()? Where does the hub connection belong?

Thanks.


Solution

  • My understanding is the VM and code behind are essentially the same (VM is a decoupled version of code behind so that you can re-use it with other screens, but in the real world that rarely happens - not worth the boilerplate IMO). I would suggest putting it wherever it is easiest for you to maintain and future developers to find.