SseReconnect class final
The reconnection rules of an event stream: whether, after how long and how many times it connects again when its connection ends or breaks.
An event stream is meant to last, but connections drop: a proxy times
out, the phone changes networks, the server restarts. A browser's
EventSource reconnects by itself, and so does
connectServerSentEvents, following these rules. It waits delay, or
the delay the server sent in a retry: field, and sends the last event
id in Last-Event-ID so the server can resume.
Feign clients build one from their configuration (the
event-stream.reconnect, event-stream.reconnect-delay and
event-stream.max-attempts keys), so applications set properties
rather than create it. You write one when you call
connectServerSentEvents yourself.
// Retry quickly, but give up after five connections in a row that
// bring no event.
const SseReconnect(delay: Duration(seconds: 1), maxAttempts: 5);
// A one-shot stream: it ends with its first connection.
SseReconnect.never;
Spring (Java): no built-in reconnection; the usual way is
Flux.retryWhen(Retry.fixedDelay(...)) on a WebClient stream.
Constructors
- SseReconnect({bool enabled = true, Duration delay = const Duration(seconds: 3), int? maxAttempts, bool retryIf(Object error) = _isTransportError})
-
Rules that reconnect when
enabled(the default), afterdelay(3 seconds by default).maxAttemptslimits the connections in a row that bring no event (null, the default: no limit).retryIfpicks the errors worth another try; by default only an HttpTransportException, that is a network failure or a timeout.const
Properties
- delay → Duration
-
The wait before connecting again. Once the server has sent a
retry:field, its value is used instead.final - enabled → bool
-
Whether to reconnect at all. When
falsethe other fields do not matter.final - hashCode → int
-
The hash code for this object.
no setterinherited
- maxAttempts → int?
-
How many connections in a row may end without bringing an event
before the stream gives up and ends (with the last error, if there
was one). The first connection counts, and any event with data
starts the count again.
nullfor no limit.final - retryIf → bool Function(Object error)
-
Decides whether a failure is worth another try. An error it rejects
ends the stream with that error, such as a
4xxthatconnectturned into an exception. A connection that simply ends is always retried.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- never → const SseReconnect
- Rules that never reconnect: the stream ends when its first connection ends, and fails with its error.