My websocket client application was built based on the above example. Now I experience a bottleneck where one websocket connection subscribes too many channels from server and the server has a limit on how many messages a single connection can receive. In order to fix the issues, the solution is to load balance multiple channels across a few websocket connections. The platform I am using is Linux.
Question> Does the boost::beast framework offer a way so that I can easily add support for multiple websocket connections without locks(i.e. still use the asynchronous operations shown in above example). It is best if there is an example to illustrate the basic idea.
class WebSocketConnection {
...
void on_read(beast::error_code ec, std::size_t) {
if (ec)
return fail(ec, "read");
// trigger MainClass::message_callback
msg_callback(std::move(buffered_message_from_server));
// continue to read further messages from server
}
...
}
class MainClass {
void message_callback(std::string msg) {
}
...
WebSocketConnection websocket_;
};
Updated:
One solution I could figure out is follows but I am not sure whether this is applicable.
class WebSocketConnection {
...
void on_read(beast::error_code ec, std::size_t) {
if (ec)
return fail(ec, "read");
// trigger MainClass::message_callback
msg_callback(std::move(buffered_message_from_server));
// continue to read further messages from server
}
...
}
class MainClass {
void message_callback(std::string msg) {
}
...
std::vector<WebSocketConnection> websockets_;
};
Basically, I define a vector of WebSocketConnection within MyClass. Each instance of WebSocketConnection will be registered a same callback function(i.e. MainClass::message_callback). Also each instance of WebSocketConnection will be passed the same net::io_context and net::strandnet::io_context::executor_type, so that all message_callback from the pool of websocket will be a sequential event.
Question> Can I do this setup without introducing race condition?
Thank you