i'm trying to implement WebSocket in flutter using clean architecture my problem is how to get data from data source cuz in the case of using rest APIs you simply request and await data and get it from the data layer but in real-time apps such as using web socket how do I return data continuously without requesting, in which layer should I put WebSocket code, how to get data and sent it to bloc?
how to implement WebSocket with clean architecture and bloc library flutter?
So, websockets need to know when they are initialized, if you dont want to make a request directly to connect a socket, you can use initState to enable connection.
For clean porpouse, on data layer , domain , infra , you dont expect Future methods, change "Future" to "Stream", and listen to them.
- Do not forget to call "your_stream".close() inside dispose method.
hi thanks for the answer, I finally did it I open WebSocket in the data layer and return the steam of WebSocket and in my presenter layer in my bloc, I use that stream then when the bloc is going to dispose I close the stream I think it the right way so thanks for the answer –
Estrange
Very good idea to return the stream instead the value or a future. –
Cortisone
@Estrange can you please share a basic structure of the application, and how you allow communication between the different layers in your app? –
Weiner
© 2022 - 2024 — McMap. All rights reserved.