SSEClient.http constructor

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

Factory constructor that always returns the HTTP-based implementation, on every platform including web.

On web, the SSEClient default factory uses the browser native EventSource, which cannot send a request body, an HTTP method other than GET, or custom headers, and which cannot report HTTP errors. This constructor uses package:http, which streams with fetch() in the browser, so it supports all of these. Use it when an application needs those features on web.

On web, a custom header must be allowed by the CORS configuration of the server, or the browser blocks the request. On web, the default headers are accept and cache-control. The user-agent header is left out, because a browser forbids it. The cache-control header stops a browser from serving a cached stream response on a reconnect. It is not CORS-safelisted, so the browser sends a preflight request, which the server must allow. The LaunchDarkly streaming service allows it. This matches the LaunchDarkly browser SDK. Any header in headers is still sent.

The headers, connectTimeout, readTimeout, body, httpMethod and uriProvider arguments are used on every platform. The other arguments have the same meaning as in the default factory.

Implementation

factory SSEClient.http(Uri uri, Set<String> eventTypes,
    {Map<String, String> headers = const {},
    Duration connectTimeout = defaultConnectTimeout,
    Duration readTimeout = defaultReadTimeout,
    String? body,
    SseHttpMethod httpMethod = SseHttpMethod.get,
    EventSourceLogger? logger,
    Uri Function()? uriProvider}) {
  return http_client.HttpSseClient(
      uri,
      eventTypes,
      httpSseClientHeaders(headers),
      connectTimeout,
      readTimeout,
      body,
      httpMethod.toString(),
      logger,
      uriProvider: uriProvider);
}