SSEClient constructor

SSEClient(
  1. Uri uri,
  2. Set<String> eventTypes, {
  3. Map<String, String> headers = defaultHeaders,
  4. Duration connectTimeout = defaultConnectTimeout,
  5. Duration readTimeout = defaultReadTimeout,
  6. String? body,
  7. SseHttpMethod httpMethod = SseHttpMethod.get,
  8. EventSourceLogger? logger,
  9. Uri uriProvider()?,
})

Factory constructor to return the platform implementation.

On all platforms, the uri and eventTypes arguments are required. On majority of platforms, the optional arguments are used. On web, the headers, connectTimeout, readTimeout, body, and httpMethod arguments are not used; the standard EventSource does not support them.

The uri specifies where to connect. The eventTypes determines which event types will be emitted. For non-web platforms, pass in headers to customize the HTTP headers of the connection request. The connectTimeout is how long to try establishing the connection and the readTimeout is how long the connection can be silent before it is torn down.

An optional body. It is recommended only to use the body with REPORT or POST methods. A GET accompanied by a body is non-standard. On html platforms the body will be ignored, as the html implementation uses the standard EventSource which does not support a body.

An optional httpMethod, if not included then the GET method will be used. On html platforms the httpMethod will be ignored, as the html implementation uses the standard EventSource which only uses GET.

An optional logger for controlling logging output from the SSE client. If not provided, a NoOpLogger will be used.

An optional uriProvider. When provided, it is invoked before every connection attempt -- the first connect and each automatic reconnect -- and its result is used for that attempt; uri is used only when no provider is given. This allows query parameters to vary between attempts (e.g. a state selector that advances as data is received). Supported on all platforms.

Implementation

factory SSEClient(Uri uri, Set<String> eventTypes,
    {Map<String, String> headers = defaultHeaders,
    Duration connectTimeout = defaultConnectTimeout,
    Duration readTimeout = defaultReadTimeout,
    String? body,
    SseHttpMethod httpMethod = SseHttpMethod.get,
    EventSourceLogger? logger,
    Uri Function()? uriProvider}) {
  // merge headers so consumer gets reasonable defaults
  var mergedHeaders = <String, String>{};
  mergedHeaders.addAll(defaultHeaders);
  mergedHeaders.addAll(headers);
  return getSSEClient(uri, eventTypes, mergedHeaders, connectTimeout,
      readTimeout, body, httpMethod.toString(), logger, uriProvider);
}