Skip to content

Conversation

@bachp
Copy link
Contributor

@bachp bachp commented Nov 19, 2025

This extension allows to handle SSE events
by invoking a callback whenever an event is received.

Related documentation: libcpr/docs#59

Closes #1014

I tested this against an axum server with the following example:

#include <cpr/cpr.h>
#include <iostream>

int main() {
    std::cout << "Connecting to SSE endpoint at http://localhost:9976/hello..." << std::endl;

    cpr::Session session;
    session.SetUrl(cpr::Url{"http://localhost:9976/hello"});

    // Set up SSE callback to print events to console
    session.SetServerSentEventCallback(
        cpr::ServerSentEventCallback{
            [](cpr::ServerSentEvent&& event, intptr_t userdata) {
                std::cout << "=== New Event ===" << std::endl;
                std::cout << "Event Type: " << event.event << std::endl;
                std::cout << "Data: " << event.data << std::endl;

                if (event.id.has_value()) {
                    std::cout << "ID: " << event.id.value() << std::endl;
                }

                if (event.retry.has_value()) {
                    std::cout << "Retry: " << event.retry.value() << " ms" << std::endl;
                }

                std::cout << std::endl;

                // Return true to continue receiving events
                return true;
            }
        }
    );

    cpr::Response response = session.Get();

    std::cout << "Connection closed." << std::endl;
    std::cout << "Status Code: " << response.status_code << std::endl;

    return 0;
}

This extension allows to handle SSE events
by invoking a callback whenever an event is received.
Copy link
Member

@COM8 COM8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bachp Thank you very much for contributing this!
I only have a minor comment and clang-tidy is not yet completely happy. The abseil-string-find-str-contains clang-tidy error can be added to the ignore list inside .clang-tidy.
The other CI runs are currently expected to fail.

@bachp bachp force-pushed the sse branch 2 times, most recently from bd73396 to 32f8795 Compare November 22, 2025 22:44
@bachp
Copy link
Contributor Author

bachp commented Nov 24, 2025

I fixed all the linter errors.

@COM8 COM8 added this to the CPR 1.14.0 milestone Nov 29, 2025
@COM8
Copy link
Member

COM8 commented Nov 29, 2025

@bachp Please rabase and then we are ready to go.

@COM8 COM8 merged commit 99f140d into libcpr:master Nov 29, 2025
34 of 37 checks passed
@COM8
Copy link
Member

COM8 commented Nov 29, 2025

@bachp I rebased and then created a new release: https://github.com/libcpr/cpr/releases/tag/1.14.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handling SSE

2 participants