C++ WebSocket Library: Complete Overview and Usage Examples
WebSocket is a full-duplex communication protocol over TCP that provides a persistent connection between a client and a server. Unlike HTTP, WebSocket allows sending messages in both directions without constant repeated requests, making it ideal for chats, online games, stock quotes, and other real-time applications.
In C++, there are several mature libraries for working with WebSocket. In this article, we will look at three of the most popular options: Boost.Beast, uWebSockets, and libwebsockets. We will analyze their features, installation, and provide working code examples.
1. Boost.Beast — Powerful and Standardized
Boost.Beast is a library based on Boost.Asio that implements HTTP and WebSocket protocols. It is part of Boost (starting from version 1.66) and is the de facto standard for serious C++ projects.
Installation
# Installing Boost (Ubuntu/Debian)sudo apt install libboost-all-dev
# Or build from source with the --with-beast optionWebSocket Server Example (Echo Server)
#include <boost/beast/core.hpp>#include <boost/beast/websocket.hpp>#include <boost/asio/ip/tcp.hpp>#include <iostream>#include <string>
namespace beast = boost::beast;namespace websocket = beast::websocket;namespace net = boost::asio;using tcp = net::ip::tcp;
int main() { try { net::io_context ioc; tcp::acceptor acceptor(ioc, tcp::endpoint(tcp::v4(), 8080));
while (true) { tcp::socket socket{ioc}; acceptor.accept(socket);
websocket::stream<tcp::socket> ws{std::move(socket)}; ws.accept();
for (;;) { beast::flat_buffer buffer; ws.read(buffer); std::string msg = beast::buffers_to_string(buffer.data()); std::cout << "Received: " << msg << std::endl;
ws.text(ws.got_text()); ws.write(buffer.data()); } } } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0;}WebSocket Client Example
#include <boost/beast/core.hpp>#include <boost/beast/websocket.hpp>#include <boost/asio/connect.hpp>#include <iostream>
namespace beast = boost::beast;namespace websocket = beast::websocket;namespace net = boost::asio;using tcp = net::ip::tcp;
int main() { try { net::io_context ioc; tcp::resolver resolver{ioc}; websocket::stream<tcp::socket> ws{ioc};
auto const results = resolver.resolve("localhost", "8080"); net::connect(ws.next_layer(), results.begin(), results.end()); ws.handshake("localhost", "/");
ws.write(net::buffer(std::string("Hello, server!"))); beast::flat_buffer buffer; ws.read(buffer); std::cout << "Response: " << beast::buffers_to_string(buffer.data()) << std::endl;
ws.close(websocket::close_code::normal); } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0;}2. uWebSockets — Fast and Lightweight
uWebSockets (µWebSockets) is a high-performance library written in C++11. It uses its own event loop and supports both HTTP and WebSocket. It is distinguished by minimal memory consumption and high speed.
Installation
git clone https://github.com/uNetworking/uWebSockets.gitcd uWebSocketsmakesudo make installEcho Server Example
#include <uwebsoc