Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does Response support returning to SSE #89

Closed
Dartly opened this issue Jul 3, 2024 · 2 comments
Closed

How does Response support returning to SSE #89

Dartly opened this issue Jul 3, 2024 · 2 comments
Labels
feature request Proposals for new features

Comments

@Dartly
Copy link

Dartly commented Jul 3, 2024

How does Response support returning to SSE

@Dartly Dartly added the feature request Proposals for new features label Jul 3, 2024
@javad-zobeidi javad-zobeidi self-assigned this Jul 3, 2024
@Dartly
Copy link
Author

Dartly commented Jul 3, 2024

I added the sse method

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:vania/src/http/response/stream_file.dart';

enum ResponseType {
  ...
  sse,
}

class Response {
       ...
      case ResponseType.sse:
        res.headers.contentType = ContentType.parse('text/event-stream');
        res.headers.add(HttpHeaders.cacheControlHeader, 'no-cache');
        res.headers.add(HttpHeaders.connectionHeader, 'keep-alive');
        res.headers.add(HttpHeaders.transferEncodingHeader, 'chunked');

        void writeSSE(String data) {
         res.add(utf8.encode('data: $data\n\n'));
        }

        await for (var event in data) {
          writeSSE(jsonEncode(event));
          await res.flush();
        }

        await res.close();
        break;
      default:
        res.write(data);
        await res.close();
    }
  }

  ...
  static sse(
      Stream<dynamic> eventStream, {
        int statusCode = HttpStatus.ok,
        Map<String, String> headers = const {},
      }) =>
      Response(
        data: eventStream,
        responseType: ResponseType.sse,
        httpStatusCode: statusCode,
        headers: headers,
      );
}

@javad-zobeidi javad-zobeidi moved this to In Development in Development Roadmap Jul 3, 2024
@javad-zobeidi javad-zobeidi removed their assignment Jul 3, 2024
javad-zobeidi added a commit to javad-zobeidi/framework that referenced this issue Jul 3, 2024
@javad-zobeidi javad-zobeidi moved this from In Development to Completed in Development Roadmap Jul 3, 2024
@javad-zobeidi javad-zobeidi closed this as completed by moving to Completed in Development Roadmap Jul 3, 2024
@javad-zobeidi
Copy link
Collaborator

@Dartly your code merged (#90)

Thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Proposals for new features
Projects
Status: Completed
Development

No branches or pull requests

2 participants