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

SSE (Server Sent Events) implementation #11

Closed
fcaldas opened this issue Nov 4, 2015 · 4 comments
Closed

SSE (Server Sent Events) implementation #11

fcaldas opened this issue Nov 4, 2015 · 4 comments

Comments

@fcaldas
Copy link

fcaldas commented Nov 4, 2015

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,

@kavyako
Copy link
Contributor

kavyako commented Nov 24, 2015

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.

@fcaldas
Copy link
Author

fcaldas commented Nov 26, 2015

Thanks @kavyako, the proposed solution worked.

@fcaldas fcaldas closed this as completed Nov 26, 2015
reneme pushed a commit to reneme/cpprestsdk that referenced this issue Aug 1, 2018
…ompileErrors to master

* commit '5befe09bf49c0089bc620e1e2ca15a34d0d9a3b1':
  fix not used Token compile warning
  fix unicode wstring and string conversion errors
  use ApiW from WIndows because we have LPWSTR
@vivbang
Copy link

vivbang commented Aug 17, 2018

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

@vivbang
Copy link

vivbang commented Oct 23, 2018

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.

GerHobbelt pushed a commit to GerHobbelt/cpprestsdk that referenced this issue Mar 20, 2024
…eme-and-host

Preserve original casing, don't lowercase scheme or host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants