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

[Question]Does tink_http support the SSE protocol? #139

Open
sonygod opened this issue Jun 21, 2024 · 1 comment
Open

[Question]Does tink_http support the SSE protocol? #139

sonygod opened this issue Jun 21, 2024 · 1 comment

Comments

@sonygod
Copy link

sonygod commented Jun 21, 2024

Does tink_http support the SSE protocol? Can it be implemented by combining tink_streams? If so, please give an example. I would be very grateful. The requirement comes from the protocol of openai api. Now I want to implement a similar SSE streaming return on the server side.

@sonygod
Copy link
Author

sonygod commented Jun 21, 2024

 @:keep
    static function streamResponse(req:IncomingRequest):Future<OutgoingResponse> {
        var responses = ['Hello', 'How are you?', 'This is a stream response example.'];
        var iterator = responses.iterator();
    
        var trigger = Signal.trigger();
        var stream = new SignalStream(trigger.asSignal());
    
        // 
        var headers = [
            new HeaderField('Content-Type', 'text/event-stream'),
            new HeaderField('Cache-Control', 'no-cache'),
            new HeaderField('Connection', 'keep-alive'),
            new HeaderField('Transfer-Encoding', 'chunked')
        ];
        var responseHeader = new ResponseHeader(HttpStatusCode.OK, "OK", headers);
        var response = new OutgoingResponse(responseHeader, cast stream);
    
        // 
        function next() {
            if (iterator.hasNext()) {
                var message = iterator.next();
                var chunkData = 'data: ${haxe.Json.stringify({message: message})}\n\n';
                trigger.trigger(Data(Chunk.ofString(chunkData)));
                Timer.delay(next, 1000); //
            } else {
                trigger.trigger(Data(Chunk.ofString("data: [DONE]\n\n")));
                trigger.trigger(End);
            }
        }
        next();
    
        return Future.sync(response);
    }

here is the solution,May be there is other way to do that ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant