What it is
uWebSockets is a highly efficient C++ WebSocket and HTTP library designed for scalability and minimal resource usage. It supports millions of concurrent connections and powers some of the largest real-time applications, including trading platforms and messaging systems.
uWebSockets provides an event-driven API for handling WebSocket and HTTP connections. It allows developers to build high-performance servers that can handle millions of clients concurrently.
Installation
git clone https://github.com/uNetworking/uWebSockets.git && cd uWebSockets && makeGetting started
The smallest useful thing you can do with it, and what each part means.
#include <uwebsockets/App.h>
int main() {
uWS::App().ws<false>("/", {
.open = [](auto* ws) {
std::cout << "A client connected" << std::endl;
},
.message = [](auto* ws, std::string_view message, uWS::OpCode opCode) {
ws->send("Hello, " + std::string(message), opCode);
}
}).listen(9001, [](auto* token) {
if (token) std::cout << "Listening on port 9001" << std::endl;
}).run();
}
uWS::App().get("/hello", [](auto* res, auto* req) {
res->end("Hello, HTTP!");
}).listen(8080, [](auto* token) {
if (token) std::cout << "Listening on port 8080" << std::endl;
}).run();Advanced usage
Where the library earns its place over a simpler alternative.
auto wsGroup = uWS::App().ws<false>("/chat", {
.message = [](auto* ws, std::string_view msg, uWS::OpCode opCode) {
ws->publish("chat", msg, opCode);
}
});uWS::App().ws<false>("/", {
.message = [](auto* ws, std::string_view msg, uWS::OpCode) {
if (msg.size() > 1024) {
ws->send("Payload too large");
}
}
});uWS::App().ws<false>("/", {
.open = [](auto* ws) { std::cout << "Secure connection!"; }
}).listen(9002, [](auto* token) { });uWS::App().ws<false>("/", {
.compression = uWS::SHARED_COMPRESSOR
});Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Connection dropped unexpectedly
- Implement proper `close` handlers to gracefully handle disconnections.
- SSL handshake failure
- Verify TLS certificate and key configuration for secure connections.
- High memory usage
- Use shared compressors and configure max payload sizes to avoid memory spikes.
Best practices
- Use the correct event loop (epoll/kqueue) for your platform for best performance.
- Enable per-message compression for bandwidth efficiency in large-scale apps.
- Validate incoming data to prevent abuse (e.g., large payloads, invalid frames).
- Leverage channels (`publish`/`subscribe`) for efficient broadcasting.
- Use TLS in production to secure WebSocket and HTTP traffic.
Background
Why it exists, and what it was reacting to.
uWebSockets was created by Alexander Aaltonen (known as 'alexhultman') to address the inefficiencies of existing WebSocket libraries. Written in C++, it leverages event loops, modern epoll/kqueue, and optimized memory handling to achieve industry-leading performance. Today, it is used in large-scale production systems where latency and throughput are critical.
