Skip to content

send many subscription messages with websocket client #3038

Description

@eaziz4

I'm looking to build an async/coro websocket client that will stay open indefinitely and continually subscribe/unsubscribe for more data based on the responses I get from my initial subscriptions. The attached gist is mainly a clone of the async websocket client example but needs modification to accomplish this. My desired flow is around the following where I connect to the server, subscribe for data(ws client store sthat in a map/deque/queue to be processed in a separate thread(processing will lead to subscribing for more data)), and have a separate thread that polls the received messages every second(if there are none, I'll do nothing and keep polling. I never want to close the connection). This might not be the best way to describe this, but the existing examples aren't setup for multiple subscriptions/offloading processing the received messages and any help would be appreciated. Thanks!

// connect to server
net::io_context ioc;
ssl::context ctx{ssl::context::tlsv13_client};
ctx.set_default_verify_paths();
auto session = std::make_shared<Session>(ioc, ctx);
session->connect(path.c_str(), "443", target.c_str(), "", headers);

//initial subscriptions(ws client stores received messages in a datastructure to offload work)
session->subscribe(request);
session->subscribe(request1);
session->subscribe(request2);
session->subscribe(request3);
ioc.run();

// separate thread reads 
for (const auto& message : session->messages_) {
    // process message and remove from messages_
    // process message may send more subscriptions
    if (shouldSubscribe) {
        session->subscribe(newRequest);
    }
}


https://gist.github.com/eaziz4/fb8cd541a3986f606161e2ab10854ec8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions