boostorg / boostorg/beast

send many subscription messages with websocket client

Open
#3,038 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
4.8k
Forks
694
Avg merge
12h 48m
Merged PRs (30d)
1

Description

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(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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.