Forum Xamarin.Forms

Managing SignalR Connections in Xamarin.Forms

I recently started experimenting with SignalR for real time updates from a server in Xamarin.Forms. This is my scenario: PageA and PageB both create separate connections in order to receive updates from the server. A user can navigate from PageA to PageB and currently, both connections remain alive.

1. Would it be best to disconnect PageA's connection when navigating to PageB and reconnect when PageB is popped off the navigation stack?
2. Should I be using separate connections? (Both use the same HubProxy)
3. When debugging I noticed that there's network activity on the Android client when the app is in the background (I assume this is the case for the iOS client as well). How do I effectively suspend/disconnect all connections when the app is placed in the background and resume/reconnect when it returns to the foreground? How are the Pages/ViewModels made aware of this transition?
4. Similarly, if I use a service like PubNub, where do I place this suspense-resume (subscribe/unsubscribe) logic?


  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭


    Why would a Page have a connection to anything? A Page shouldn't be aware of the connections or where data comes from: Its not the job of a Page to do that. Its the job of the ViewModel the Page uses for its BindingContext.

    The ViewModel should have a DataAccessLayer between it and the actual data. Which in the case comes from a service or database. Later if you change the source of the data you don't have to update 15 ViewModels: You only update the DAL.

    The Page then is just a reflection of the data found in a ViewModel.

    But Pages shouldn't be aware of the logic or mechanisms, and logic shouldn't be aware of UI.

  • I apologize for being unclear. The connections are in my view models and the pages' binding contexts are set to those. By PageA and PageB I was indirectly referring to their view models. Currently, I start the connection in the view model's constructor but I'm unsure of where/when to place supend/resume for when the user navigates to a different page or the app goes into the background.

Sign In or Register to comment.