Q:

Socket Connection with MVVM

I am trying to build a chat app with MVVM architecture but i don't know where to put my socket connection class, i need an idea or starting point etc.
Single Data Source Principle
I am using single data source of truth principle. So an example: If a message comes to app from socket server, my socket class should save it to db and then should serve it from db to the observers(so they will be view models) using live data.
My app architecture
My app contains different activites (chat screen,conversations screen etc.) and these activites will observe to my socket connection repository class. Also if user closes the app, my socket connection will die with the app. I don't want any background connection.
What i am asking?
What is the best way to achive this functionality: 
1) Socket class should use live data because activites will observe to it. So it should be a repository.
2) My socket class should live always while app open.
What should i use for socket repository? A service or a singleton class or something different? What do you suggest? Where should i start socket connection repository? From app class or something different?

A:

Check out the new LiveData and Lifecycle architecture of android in the new Arch components library.
With it, you can observe the LiveData object from any activity or fragment. It starts with your app, is notified about any Lifecycle changes, and dies with your app. Also: isn't destroyed on configuration changes.
The only problem is there has to be some initiation of the LiveData object. I can be done in the App class. Or in the activity if your app is a one-activity architecture.

