websocket and http client and server library, with TLS support and very few dependencies
Go to file
2024-05-17 02:02:58 -07:00
.github/workflows Update unittest_windows_gcc.yml (#494) 2023-11-20 22:28:12 -08:00
CMake Fix mbedtls-3.0 problem (#322) 2021-10-22 11:10:58 -07:00
docker (build) rename makefile to makefile.dev to ease cmake BuildExternal (fix #261) 2020-12-22 21:42:39 -08:00
docs changelog 2024-03-27 22:12:37 -07:00
ixwebsocket Version check mbedtls instead of introducing a new define (#516) 2024-05-08 11:02:00 -07:00
test Support URLs with no slash before the question mark (#507) 2024-03-18 22:26:45 -07:00
third_party (ws) bump CLI command line parsing library from 1.8 to 2.0 2021-07-27 20:48:25 +02:00
tools add tool to ease making commits 2020-07-24 11:53:09 -07:00
ws Fix DNSLookup memory leak (#422) 2022-12-22 17:13:51 -08:00
.clang-format update clang format file 2019-09-10 22:17:08 -07:00
.dockerignore (build) rename makefile to makefile.dev to ease cmake BuildExternal (fix #261) 2020-12-22 21:42:39 -08:00
.gitignore Add ping interval to constructor params for WebSocketServer (#497) 2024-03-12 09:46:27 -07:00
.pre-commit-config.yaml fix #182 2020-04-22 14:26:16 -07:00
CMakeLists.txt bump version in CMakeLists.txt 2024-03-27 22:11:15 -07:00
docker-compose.yml (ws) echo_client command renamed to autoroute. Command exit once the server close the connection. push_server commands exit once N messages have been sent. 2020-09-03 09:13:23 -07:00
Dockerfile use alpine as the docker distribution 2020-03-27 17:38:35 -07:00
httpd.cpp embedded help for httpd tool 2020-06-01 17:01:12 -07:00
ixwebsocket-config.cmake.in Update ixwebsocket-config.cmake.in (#390) 2022-04-28 23:56:00 -07:00
ixwebsocket.pc.in Fix version in CMakeLists.txt (#467) 2023-06-05 10:03:17 -07:00
LICENSE.txt First import 2018-09-27 14:57:19 -07:00
main.cpp add notes about ssl configuration in demo program 2021-05-09 13:45:01 -07:00
makefile.dev makefile: remove some install targets 2024-05-17 02:02:58 -07:00
mkdocs.yml use default mkdocs theme 2020-04-04 11:05:14 -07:00
README.md Initialize the PSA Crypto API if requested (#514) 2024-04-29 21:12:56 -07:00
SECURITY.md Create SECURITY.md 2019-10-17 06:58:22 -07:00

Hello world

(note from the main developer, sadly I don't have too much time to devote to this library anymore, maybe it's time to pass the maintenance to someone else more motivated ?)

IXWebSocket is a C++ library for WebSocket client and server development. It has minimal dependencies (no boost), is very simple to use and support everything you'll likely need for websocket dev (SSL, deflate compression, compiles on most platforms, etc...). HTTP client and server code is also available, but it hasn't received as much testing.

It is been used on big mobile video game titles sending and receiving tons of messages since 2017 (iOS and Android). It was tested on macOS, iOS, Linux, Android, Windows and FreeBSD. Two important design goals are simplicity and correctness.

/*
 *  main.cpp
 *  Author: Benjamin Sergeant
 *  Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
 *
 *  Super simple standalone example. See ws folder, unittest and doc/usage.md for more.
 *
 *  On macOS
 *  $ mkdir -p build ; (cd build ; cmake -DUSE_TLS=1 .. ; make -j ; make install)
 *  $ clang++ --std=c++11 --stdlib=libc++ main.cpp -lixwebsocket -lz -framework Security -framework Foundation
 *  $ ./a.out
 *
 *  Or use cmake -DBUILD_DEMO=ON option for other platforms
 */

#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXUserAgent.h>
#include <iostream>

int main()
{
    // Required on Windows
    ix::initNetSystem();

    // Our websocket object
    ix::WebSocket webSocket;

    // Connect to a server with encryption
    // See https://machinezone.github.io/IXWebSocket/usage/#tls-support-and-configuration
    //     https://github.com/machinezone/IXWebSocket/issues/386#issuecomment-1105235227 (self signed certificates)
    std::string url("wss://echo.websocket.org");
    webSocket.setUrl(url);

    std::cout << "Connecting to " << url << "..." << std::endl;

    // Setup a callback to be fired (in a background thread, watch out for race conditions !)
    // when a message or an event (open, close, error) is received
    webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
        {
            if (msg->type == ix::WebSocketMessageType::Message)
            {
                std::cout << "received message: " << msg->str << std::endl;
                std::cout << "> " << std::flush;
            }
            else if (msg->type == ix::WebSocketMessageType::Open)
            {
                std::cout << "Connection established" << std::endl;
                std::cout << "> " << std::flush;
            }
            else if (msg->type == ix::WebSocketMessageType::Error)
            {
                // Maybe SSL is not configured properly
                std::cout << "Connection error: " << msg->errorInfo.reason << std::endl;
                std::cout << "> " << std::flush;
            }
        }
    );

    // Now that our callback is setup, we can start our background thread and receive messages
    webSocket.start();

    // Send a message to the server (default to TEXT mode)
    webSocket.send("hello world");

    // Display a prompt
    std::cout << "> " << std::flush;

    std::string text;
    // Read text from the console and send messages in text mode.
    // Exit with Ctrl-D on Unix or Ctrl-Z on Windows.
    while (std::getline(std::cin, text))
    {
        webSocket.send(text);
        std::cout << "> " << std::flush;
    }

    return 0;
}

Interested? Go read the docs! If things don't work as expected, please create an issue on GitHub, or even better a pull request if you know how to fix your problem.

IXWebSocket is actively being developed, check out the changelog to know what's cooking. If you are looking for a real time messaging service (the chat-like 'server' your websocket code will talk to) with many features such as history, backed by Redis, look at cobra.

IXWebSocket client code is autobahn compliant beginning with the 6.0.0 version. See the current test results. Some tests are still failing in the server code.

Starting with the 11.0.8 release, IXWebSocket should be fully C++11 compatible.

Users

If your company or project is using this library, feel free to open an issue or PR to amend this list.

  • Machine Zone
  • Tokio, a discord library focused on audio playback with node bindings.
  • libDiscordBot, an easy to use Discord-bot framework.
  • gwebsocket, a websocket (lua) module for Garry's Mod
  • DisCPP, a simple but feature rich Discord API wrapper (archived as of Oct 8, 2021)
  • discord.cpp, a discord library for making bots
  • Teleport, Teleport is your own personal remote robot avatar
  • Abaddon, An alternative Discord client made with C++/gtkmm
  • NovaCoin, a hybrid scrypt PoW + PoS based cryptocurrency.
  • Candy, A WebSocket and TUN based VPN for Linux
  • ITGmania, a cross platform Dance Dance Revolution-like emulator.

Alternative libraries

There are plenty of great websocket libraries out there, which might work for you. Here are a couple of serious ones.

uvweb is a library written by the IXWebSocket author which is built on top of uvw, which is a C++ wrapper for libuv. It has more dependencies and does not support SSL at this point, but it can be used to open multiple connections within a single OS thread thanks to libuv.

To check the performance of a websocket library, you can look at the autoroute project.

Continuous Integration

OS TLS Sanitizer Status
Linux OpenSSL None Build2
macOS Secure Transport Thread Sanitizer Build2
macOS OpenSSL Thread Sanitizer Build2
macOS MbedTLS Thread Sanitizer Build2
Windows Disabled None Build2
UWP Disabled None Build2
Linux OpenSSL Address Sanitizer Build2
  • Some tests are disabled on Windows/UWP because of a pathing problem
  • TLS and ZLIB are disabled on Windows/UWP because enabling make the CI run takes a lot of time, for setting up vcpkg.