Skip to content

wsocket-io/sdk-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wSocket SDK for C++

Official C++ SDK for wSocket — realtime pub/sub, presence, history, and push notifications.

GitHub Release License: MIT

Installation

CMake (FetchContent)

include(FetchContent)
FetchContent_Declare(wsocket_io
    GIT_REPOSITORY https://github.com/wsocket-io/sdk-cpp.git
    GIT_TAG v0.1.0
)
FetchContent_MakeAvailable(wsocket_io)
target_link_libraries(your_app wsocket_io)

Manual Build

cd sdks/cpp
mkdir build && cd build
cmake ..
make
sudo make install

Dependencies

On Ubuntu/Debian:

sudo apt install libwebsocketpp-dev nlohmann-json3-dev libcurl4-openssl-dev libboost-all-dev

Quick Start

#include <wsocket/wsocket.hpp>

int main() {
    wsocket::Client client("wss://node00.wsocket.online", "your-api-key");

    client.on_connect([] {
        std::cout << "Connected!" << std::endl;
    });

    client.connect();

    auto channel = client.pubsub().channel("chat");

    channel->subscribe([](const nlohmann::json& data, const wsocket::MessageMeta& meta) {
        std::cout << "Received: " << data.dump() << std::endl;
    });

    channel->publish({{"text", "Hello from C++!"}});

    client.run(); // blocks
    return 0;
}

Presence

auto ch = client.pubsub().channel("room");

ch->presence().enter({{"name", "Alice"}});

ch->presence().on_enter([](const wsocket::PresenceMember& m) {
    std::cout << m.client_id << " entered" << std::endl;
});

ch->presence().on_leave([](const wsocket::PresenceMember& m) {
    std::cout << m.client_id << " left" << std::endl;
});

ch->presence().get();
ch->presence().on_members([](const std::vector<wsocket::PresenceMember>& members) {
    std::cout << "Online: " << members.size() << std::endl;
});

History

ch->history(50);
ch->on_history([](const wsocket::HistoryResult& result) {
    for (const auto& msg : result.messages) {
        std::cout << msg.publisher_id << ": " << msg.data.dump() << std::endl;
    }
});

Push Notifications

wsocket::PushClient push("https://node00.wsocket.online", "secret", "app1");

push.register_fcm("device-token", "user-123");
push.send_to_member("user-123", {{"title", "Hello"}, {"body", "World"}});
push.broadcast({{"title", "News"}, {"body", "Update available"}});

Requirements

  • C++17 or later
  • Boost 1.70+ (for Beast) or websocketpp
  • nlohmann/json 3.0+
  • libcurl 7.0+

License

MIT

About

wSocket.io cpp SDK

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors