How to make websocket stream broadcast to many other pages?
Asked Answered
G

3

11

I have a websocket stream being listened:

widget.channel.stream.listen((data) {
  print("!!!!new msg: $data");
  var dataJson = json.decode(data);
  print(dataJson["content"]);
  // do my job
  setState(() {
    _allAnimateMessages.insert(0, newMsg);
  });
  newMsg.animationController.forward();
});

But, when enter that page again, there was an error says: Bad state: Stream has already been listened to.

How to make it as broadcast and other pages can receive that broadcast?

Gilles answered 23/5, 2018 at 7:40 Comment(1)
Can you provide more hints what you are using: - What libraries/packages - What is channel? - Where is this body of code located in your application?Twentieth
W
8

Solution for package web_socket_channel:

final channel = IOWebSocketChannel.connect(socketUrl);
final streamController = StreamController.broadcast();
streamController.addStream(channel.stream);

After that simply use streamController.stream to listen web socket events.

W answered 29/6, 2020 at 11:8 Comment(0)
H
4

You can use broadcasts.

 //Here is the solution
 StreamController<String> streamController = new StreamController.broadcast();   //Add .broadcast here

//Now you can listen from various places
@override
void initState() {
  super.initState();

  print("Creating a StreamController...");
  //First subscription
  streamController.stream.listen((data) {
    print("DataReceived1: " + data);
  }, onDone: () {
    print("Task Done1");
  }, onError: (error) {
    print("Some Error1");
  });
  //Second subscription
  streamController.stream.listen((data) {
    print("DataReceived2: " + data);
  }, onDone: () {
    print("Task Done2");
  }, onError: (error) {
    print("Some Error2");
  });

  streamController.add("This a test data");
  print("code controller is here");

}

Font: https://medium.com/@ayushpguptaapg/using-streams-in-flutter-62fed41662e4

When using broadcasts you can have multiple listeners in the same stream.

If you simply use a stream without ".broadcast ()" you can only have one listener

Humanitarian answered 30/11, 2019 at 20:38 Comment(1)
to add a message we must call streamController.add(), so how to get the reference object of streamController on another class or page ?Jen
G
0

As there are no useful answer, I update my answer here for other reference.

  1. duplicate subscribe stream is a desired behaviour in flutter.

if you are just using a StreamBuilder, the stream can be listen only once. think about it, if your stream can be listen to many other pages or widgets, then data would be repeated.

  1. But if you want using one single stream, and update all widgets

this is really can be occured when develop a complicated app, for example, you are building a chat app, new message comes, you should update many pages UI (your dialog chat ui, your session list ui....), then you should subscribe this streams in many pages, I still not found a proper way to do this, except make this stream to be broadcast, and do your work.

Gilles answered 5/10, 2018 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.