Convert Figma logo to code with AI

zaphoyd logowebsocketpp

C++ websocket client/server library

6,996
1,969
6,996
455

Top Related Projects

22,033

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

Simple, secure & standards compliant web server for the most demanding of applications

A curated list of Websocket libraries and resources.

4,316

HTTP and WebSocket built on Boost.Asio in C++11

C++11 implementation of Socket.IO client

Quick Overview

WebSocket++ is a header-only C++ library that implements RFC6455 (The WebSocket Protocol). It provides a flexible and standards-compliant WebSocket client and server implementation, allowing developers to easily integrate WebSocket functionality into their C++ applications.

Pros

  • Header-only library, simplifying integration and deployment
  • Cross-platform support (Windows, Linux, macOS)
  • Extensive documentation and examples
  • Supports both client and server implementations

Cons

  • Steep learning curve for beginners due to C++ complexity
  • Requires C++11 or later
  • Limited built-in support for SSL/TLS (requires external libraries)
  • May have performance overhead compared to lower-level implementations

Code Examples

  1. Creating a WebSocket server:
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>

using websocketpp::server;
using websocketpp::config::asio;

int main() {
    server<asio> ws_server;
    ws_server.init_asio();
    ws_server.listen(9002);
    ws_server.start_accept();
    ws_server.run();
    return 0;
}
  1. Handling WebSocket connections:
server<asio> ws_server;

ws_server.set_open_handler([](websocketpp::connection_hdl hdl) {
    std::cout << "New connection opened" << std::endl;
});

ws_server.set_message_handler([](websocketpp::connection_hdl hdl, server<asio>::message_ptr msg) {
    std::cout << "Received message: " << msg->get_payload() << std::endl;
});
  1. Sending a message to a client:
server<asio>::connection_ptr con = ws_server.get_con_from_hdl(hdl);
ws_server.send(con, "Hello, client!", websocketpp::frame::opcode::text);

Getting Started

  1. Clone the repository:

    git clone https://github.com/zaphoyd/websocketpp.git
    
  2. Include the WebSocket++ headers in your project:

    #include <websocketpp/server.hpp>
    #include <websocketpp/config/asio_no_tls.hpp>
    
  3. Compile your project with C++11 or later:

    g++ -std=c++11 your_file.cpp -I/path/to/websocketpp -lboost_system -lpthread
    

Note: WebSocket++ requires Boost.Asio for networking functionality. Make sure to install Boost and link against the necessary libraries when compiling your project.

Competitor Comparisons

22,033

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

Pros of gorilla/websocket

  • Written in Go, offering better performance and concurrency support
  • Simpler API and easier to use for Go developers
  • More active maintenance and community support

Cons of gorilla/websocket

  • Limited to Go programming language
  • Fewer advanced features compared to websocketpp

Code Comparison

websocketpp (C++):

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>

typedef websocketpp::server<websocketpp::config::asio> server;

int main() {
    server echo_server;
    echo_server.set_message_handler(&on_message);
    echo_server.init_asio();
    echo_server.listen(9002);
    echo_server.start_accept();
    echo_server.run();
}

gorilla/websocket (Go):

import (
    "github.com/gorilla/websocket"
    "net/http"
)

func main() {
    http.HandleFunc("/ws", handleWebSocket)
    http.ListenAndServe(":8080", nil)
}

func handleWebSocket(w http.ResponseWriter, r *http.Request) {
    conn, _ := upgrader.Upgrade(w, r, nil)
    // Handle WebSocket connection
}

websocketpp offers more flexibility and advanced features, while gorilla/websocket provides a simpler API and better integration with Go's ecosystem. The choice between them depends on the programming language preference and specific project requirements.

Simple, secure & standards compliant web server for the most demanding of applications

Pros of uWebSockets

  • Higher performance and lower latency
  • Supports both WebSocket and HTTP protocols
  • More active development and maintenance

Cons of uWebSockets

  • Less flexible and customizable
  • Steeper learning curve for beginners
  • Limited documentation compared to websocketpp

Code Comparison

websocketpp:

#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>

websocketpp::server<websocketpp::config::asio> server;
server.init_asio();
server.set_message_handler([](auto hdl, auto msg) {
    // Handle message
});

uWebSockets:

#include <uwebsockets/App.h>

uWS::App().ws<PerSocketData>("/*", {
    .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
        // Handle message
    }
}).listen(9001, [](auto *listen_socket) {
    if (listen_socket) {
        std::cout << "Listening on port " << 9001 << std::endl;
    }
}).run();

Both libraries provide WebSocket functionality, but uWebSockets offers a more concise API and built-in support for HTTP. websocketpp provides more granular control over the WebSocket implementation, while uWebSockets focuses on performance and ease of use. The choice between them depends on specific project requirements and developer preferences.

A curated list of Websocket libraries and resources.

Pros of awesome-websockets

  • Curated list of WebSocket resources, libraries, and tools across multiple languages and platforms
  • Provides a comprehensive overview of the WebSocket ecosystem
  • Regularly updated with community contributions

Cons of awesome-websockets

  • Not a library or implementation, just a collection of links
  • Lacks direct code examples or implementation guidance
  • May include outdated or deprecated resources without frequent maintenance

Code comparison

Not applicable, as awesome-websockets is a curated list and doesn't contain actual code implementations.

websocketpp, on the other hand, is a C++ WebSocket library. Here's a basic example of creating a WebSocket server using websocketpp:

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>

typedef websocketpp::server<websocketpp::config::asio> server;

int main() {
    server echo_server;
    echo_server.init_asio();
    echo_server.listen(9002);
    echo_server.start_accept();
    echo_server.run();
}

Summary

awesome-websockets serves as a valuable resource for developers looking to explore various WebSocket implementations and tools across different languages and platforms. It provides a broad overview of the WebSocket ecosystem but doesn't offer direct implementation guidance.

websocketpp, in contrast, is a specific C++ library for implementing WebSocket functionality, offering direct code solutions for developers working with C++.

4,316

HTTP and WebSocket built on Boost.Asio in C++11

Pros of Beast

  • Part of the Boost C++ Libraries, offering better integration with other Boost components
  • Provides a more comprehensive networking toolkit, including HTTP and WebSocket support
  • Actively maintained with regular updates and improvements

Cons of Beast

  • Steeper learning curve due to its broader feature set
  • Heavier dependency footprint as part of the Boost ecosystem
  • May be overkill for simple WebSocket-only projects

Code Comparison

WebSocketpp:

#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>

typedef websocketpp::server<websocketpp::config::asio> server;

server echo_server;
echo_server.set_message_handler([](websocketpp::connection_hdl hdl, server::message_ptr msg) {
    // Handle message
});

Beast:

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>

namespace websocket = boost::beast::websocket;
websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
ws.accept();
ws.async_read(buffer, [](boost::system::error_code ec, std::size_t bytes_transferred) {
    // Handle message
});

Both libraries offer WebSocket functionality, but Beast provides a more comprehensive networking toolkit as part of the Boost ecosystem. WebSocketpp is more focused on WebSocket-specific implementations, making it potentially simpler for projects that only require WebSocket support. Beast's integration with other Boost libraries can be advantageous for larger projects, while WebSocketpp might be preferable for lightweight, WebSocket-centric applications.

C++11 implementation of Socket.IO client

Pros of socket.io-client-cpp

  • Built specifically for Socket.IO protocol, offering seamless integration with Socket.IO servers
  • Supports automatic reconnection and event handling out of the box
  • Provides a higher-level abstraction for real-time communication

Cons of socket.io-client-cpp

  • Limited to Socket.IO protocol, less flexible for generic WebSocket applications
  • May have a steeper learning curve for developers unfamiliar with Socket.IO concepts
  • Potentially higher overhead due to additional Socket.IO-specific features

Code Comparison

websocketpp:

websocketpp::client<websocketpp::config::asio_client> c;
c.init_asio();
c.set_message_handler([](websocketpp::connection_hdl, client::message_ptr msg) {
    std::cout << "Received: " << msg->get_payload() << std::endl;
});

socket.io-client-cpp:

sio::client h;
h.connect("http://localhost:3000");
h.socket()->on("event_name", [&](sio::event& ev) {
    std::cout << "Received: " << ev.get_message()->get_string() << std::endl;
});

Both libraries provide C++ implementations for WebSocket communication, but websocketpp offers a more generic WebSocket solution, while socket.io-client-cpp is tailored specifically for the Socket.IO protocol. The choice between them depends on the specific requirements of your project and whether you need Socket.IO-specific features or a more flexible WebSocket implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

WebSocket++ (0.8.2)

WebSocket++ is a header only C++ library that implements RFC6455 The WebSocket Protocol. It allows integrating WebSocket client and server functionality into C++ programs. It uses interchangeable network transport modules including one based on raw char buffers, one based on C++ iostreams, and one based on Asio (either via Boost or standalone). End users can write additional transport policies to support other networking or event libraries as needed.

Major Features

  • Full support for RFC6455
  • Partial support for Hixie 76 / Hybi 00, 07-17 draft specs (server only)
  • Message/event based interface
  • Supports secure WebSockets (TLS), IPv6, and explicit proxies.
  • Flexible dependency management (C++11 Standard Library or Boost)
  • Interchangeable network transport modules (raw, iostream, Asio, or custom)
  • Portable/cross platform (Posix/Windows, 32/64bit, Intel/ARM/PPC)
  • Thread-safe

Get Involved

Build Status

Project Website http://www.zaphoyd.com/websocketpp/

User Manual http://docs.websocketpp.org/

GitHub Repository https://github.com/zaphoyd/websocketpp/

GitHub pull requests should be submitted to the develop branch.

Announcements Mailing List http://groups.google.com/group/websocketpp-announcements/

IRC Channel #websocketpp (freenode)

Discussion / Development / Support Mailing List / Forum http://groups.google.com/group/websocketpp/

Author

Peter Thorson - websocketpp@zaphoyd.com