how to implement WebSocket with clean architecture and bloc library flutter?

808

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.
Share:
808
reza47
Author by

reza47

Developing apps using react for web and flutter for mobile and desktop

Updated on December 26, 2022

Comments

  • reza47
    reza47 over 1 year

    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?

  • reza47
    reza47 almost 3 years
    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
  • Filipe Piletti Plucenio
    Filipe Piletti Plucenio over 2 years
    Very good idea to return the stream instead the value or a future.