BoraPushClient.connect constructor

BoraPushClient.connect({
  1. required Uri uri,
  2. bool withCredentials = false,
  3. bool closeOnError = true,
})

Connect to an SSE endpoint with the specified URI and options.

Implementation

factory BoraPushClient.connect({
	required Uri uri,
	bool withCredentials = false,
	bool closeOnError = true,
}) {
	// print('SSE connect...');
	final streamController = StreamController<String>();
	final eventSource = html.EventSource(uri.toString(), withCredentials: withCredentials);

	eventSource.addEventListener('message', (html.Event message) {
	// print('SSE Event listener...');
	streamController.add((message as html.MessageEvent).data as String);
	});

	/// Close if the endpoint is not working
	if (closeOnError) {
	eventSource.onError.listen((event) {
		// print('On error...');
		eventSource?.close();
		streamController?.close();
	});
	}

	return BoraPushClient._internal(eventSource, streamController);
}