Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
This repository was archived by the owner on Jun 1, 2026. It is now read-only.

SSE (Server Sent Events) implementation #11

Description

@fcaldas

Hi, I would like to know if it is possible to implement an SSE protocol on top of Casablanca, the main idea would be to send multiple replies to an web::http::http_request object whenever new data arrives to be distributed to a connected client, since I cannot use the reply() method multiple times what would be the right way to do it?

I have also tried to use the set_response_stream method on web::http::http_request but I have an exception in this case. Is it doable using Casablanca?

    void wwwReports(web::http::http_request request){
        json::value reply;
        Concurrency::streams::basic_ostream<unsigned char> dataStream;
        request.set_response_stream(dataStream);
        dataStream.write("data: ");
        dataStream.write("test1\n");
    }

Thanks for your attention,

Activity

  1. kavyako commented on Nov 24, 2015

    @kavyako
    Contributor

    The exception could be coming from when our internal implementation tries to read from the dataStream (since it is an output stream).

    You might want to use a producer_consumer_buffer here. Then you can write to the buffer and our internal implementation can read from the buffer and send the data.

       streams::producer_consumer_buffer<char> rwbuf;
    
        streams::basic_istream<uint8_t> stream(rwbuf);
        response.set_body(stream);
    
        auto rep = request.reply(response);
    
        rwbuf.putn_nocopy("data", length).wait();
        rwbuf.putn_nocopy("data2", length2).wait();
        rwbuf.sync().wait();
        rwbuf.putn_nocopy("data3", length).wait();
        rwbuf.close(std::ios_base::out).wait();
        rep.wait();
    

    Here, calling sync() will ensure the data is sent out and closing the buffer will complete the response.

  2. fcaldas commented on Nov 26, 2015

    @fcaldas
    Author

    Thanks Kavya Kotacherry - MSFT (@kavyako), the proposed solution worked.

  3. added a commit that references this issue on Aug 1, 2018
  4. vivbang commented on Aug 17, 2018

    @vivbang

    Implementation of SSE(Server sent event ) is possible using rest HTTP servers ?

  5. vivbang commented on Oct 23, 2018

    @vivbang

    Hi ... I was able to implement Server Sent Event(SSE) using above proposed solution.
    Now I am stuck on how to determine whether underlying connection is still active before writing SSE data on stream.
    It is very urgent can u please suggest a way determine whether SSE connection is active or not.

  6. added a commit that references this issue on Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions