Single-subscription streams versus broadcast streams

The Dart stream framework supports the single-subscription and broadcast streams in order to have different approaches depending on the required solution. Let's see the difference between single-subscription and broadcast streams.

A single-subscription stream

A stream that allows you to have only one listener during the entire lifetime is called a single-subscription stream. You are not allowed to cancel and subscribe to the same stream again. The newly created stream starts generating events only after the subscription starts listening to them. It stops generating events after the subscription is canceled even if the stream can provide more. The single-subscription stream always delivers each event in the correct order to a listener.

Note

Use single-subscription streams when the event delivery and its order are important for your solution.

A good usage example of a single-subscription stream is getting data from the file or server.

A broadcast stream

A stream that allows you to have multiple listeners during the entire lifetime is called the broadcast stream. You are allowed to subscribe or cancel the subscription at any time. A broadcast stream starts to generate events immediately after the creation if it ready, independent of whether any subscription is registered or not. This fact means that some portion of the events can be lost in a moment when no listener is registered. There is no guarantee that multiple listeners will get the event in the same order that they were registered. A broadcast stream only guarantees that each listener will get all the events in the correct order.

Note

Use broadcast streams when delivering events to multiple listeners is important for your solution.

A good example of using the broadcast stream is the eventbus implementation that supports multiple listeners.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.249.210