VRTQL WebSockets Library
Description
VRTQL WebSockets is a robust, performance-oriented WebSocket library written in
C. It provides a simple yet flexible API for building both WebSocket clients
and servers, and it supports all standard WebSocket features: text and binary
messages, ping/pong and control frames, and built-in OpenSSL (wss://) support.
The library has two parts, built from completely different networking designs,
each suited to its use case:
- A client-side component designed for single connections that operates
synchronously (blocking with an optional timeout), feeling like a traditional
socket API. It uses the operating system's native
poll() and requires no
additional libraries.
- An optional server-side component designed for many concurrent
connections that operates asynchronously atop
libuv,
with a worker-thread pool for processing.
On top of the client API there is also an optional messaging layer that adds
a structured message format with JSON and MessagePack serialization — a small
step toward AMQP/MQTT-style messaging without the heft.
Why vws?
- Portable C — compiles and runs on Linux, FreeBSD, NetBSD, OpenBSD, OS X,
Illumos/Solaris and Windows. The CMake build supports cross-compiling from
Linux/BSD to Windows with MinGW.
- MIT licensed — usable in commercial, closed-source applications.
- Dual client/server — a synchronous client and an asynchronous,
multithreaded server, with the server component entirely optional.
- Optional messaging layer — JSON and MessagePack over the same connection.
- Hardened — bounds and NULL guards on the wire-facing parser and a
frame-size cap to resist wire-reachable denial-of-service inputs.
Quick Start
The fastest path to a working client and a working server. Both examples are
complete, compilable programs; the fuller variants appear under
Usage.
A minimal client
This connects, sends a text message, waits for a reply, and disconnects.
// quickstart-client — minimal WebSocket client (subset of client-basic)
#include <vws/websocket.h>
#include <stdio.h>
int main(int argc, const char* argv[])
{
// Create a connection object
vws_cnx* cnx = vws_cnx_new();
// Connect. TLS is used automatically if the "wss" scheme is given.
if (vws_connect(cnx, "ws://localhost:8181/websocket") == false)
{
printf("Failed to connect\n");
vws_cnx_free(cnx);
return 1;
}
// Send a TEXT frame
vws_frame_send_text(cnx, "Hello, world!");
// Receive a message (returns NULL on timeout)
vws_msg* reply = vws_msg_recv(cnx);
if (reply != NULL)
{
printf("Received %zu bytes\n", reply->data->size);
vws_msg_free(reply);
}
vws_disconnect(cnx);
vws_cnx_free(cnx);
return 0;
}
A minimal WebSocket echo server
Before you begin: the server component requires libuv and is built only
when BUILD_SERVER is enabled (the default). See Installation.
This accepts connections and echoes each message back.
// quickstart-server — minimal WebSocket echo server (subset of server-ws)
#include <vws/server.h>
// Runs in the context of a worker thread. Assigned to the derived process_ws
// hook (on_msg_in is the framework's internal dispatcher — do not overwrite it).
void echo(vws_svr* s, vws_cid_t cid, vws_msg* m, void* ctx)
{
// Echo the message back, preserving its opcode (TEXT/BINARY)
vws_msg* reply = vws_msg_new();
reply->opcode = m->opcode;
vws_buffer_append(reply->data, m->data->data, m->data->size);
// send() takes ownership of the reply; we free the incoming message
s->send(s, cid, reply, NULL);
vws_msg_free(m);
}
int main(int argc, const char* argv[])
{
vws_svr* server = vws_svr_new(10, 0, 0);
server->process_ws = echo;
// Run (blocks until the server is stopped)
vws_svr_run(server, "127.0.0.1", 8181);
// Stop via the base type, then free and clean up
vws_tcp_svr_stop((vws_tcp_svr*)server);
vws_svr_free(server);
vws_cleanup();
return 0;
}
To see the library exercised end-to-end, build the project and step into the
src/test directory: run ./server to start a simple WebSocket server, then run
test_websocket.
Concepts
Unlike traditional HTTP connections, which are stateless and unidirectional,
WebSocket connections are stateful and bidirectional. A connection is
established through an HTTP handshake (an HTTP Upgrade request), then upgraded to
a WebSocket connection if the server supports it, and remains open until
explicitly closed.
The protocol communicates through data units called frames. The protocol
permits a frame payload of up to 2^64 bytes, though the effective limit is
usually smaller due to network or system constraints. In addition, this library
caps a single frame at 64 MiB (VWS_MAX_FRAME_SIZE): a frame whose payload
length exceeds that is rejected as a FRAME_ERROR. The cap is a compile-time
tunable defined in src/websocket.c.
While frames are the unit on the wire, applications generally work in terms of
messages — one or more frames terminated by a frame with the FIN bit set.
The deeper frame taxonomy (opcodes, masking, control frames) is covered under
Advanced Topics. For the full reference, see the
online documentation.
Usage
The examples below are each complete, standalone programs. They progress from
the synchronous client API, through the optional messaging layer, to the
asynchronous server API.
Client API
The client API is built solely on WebSocket constructs — frames, messages and
connections. The connection object (vws_cnx) embeds a vws_socket as its first
member, which is why socket-level calls such as vws_socket_set_timeout() and
vws_socket_is_connected() take the connection cast to (vws_socket*).
// client-basic — synchronous WebSocket client
#include <vws/websocket.h>
#include <stdio.h>
#include <assert.h>
int main(int argc, const char* argv[])
{
// Create connection object
vws_cnx* cnx = vws_cnx_new();
// Set connection timeout to 2 seconds (the default is 10). This applies
// both to connect() and to read operations (i.e. poll()).
vws_socket_set_timeout((vws_socket*)cnx, 2);
// Connect. This will automatically use SSL if the "wss" scheme is used.
cstr uri = "ws://localhost:8181/websocket";
if (vws_connect(cnx, uri) == false)
{
printf("Failed to connect to the WebSocket server\n");
vws_cnx_free(cnx);
return 1;
}
// Check connection state. Should be true here as we just connected.
assert(vws_socket_is_connected((vws_socket*)cnx) == true);
// Enable tracing. This dumps frames to the console in human-readable
// format as they are sent and received.
vws.tracelevel = VT_PROTOCOL;
// Send a TEXT frame
vws_frame_send_text(cnx, "Hello, world!");
// Receive a websocket message (NULL on timeout)
vws_msg* reply = vws_msg_recv(cnx);
if (reply != NULL)
{
vws_msg_free(reply);
}
// Send a BINARY message
vws_msg_send_binary(cnx, (ucstr)"Hello, world!", 14);
// Receive a websocket message (NULL on timeout)
reply = vws_msg_recv(cnx);
if (reply != NULL)
{
vws_msg_free(reply);
}
vws_disconnect(cnx);
vws_cnx_free(cnx);
return 0;
}
Messaging API
The messaging API is built on top of the client API. WebSockets provide
real-time bidirectional communication but do not, by themselves, offer the
structure of heavier protocols like AMQP. The messaging layer adds a structured
message (vrtql_msg) with two string maps — routing (for routing information)
and headers (for application use) — plus a payload that can hold text or
binary data. It handles serialization automatically and can auto-detect each
incoming message's format, so JSON and MessagePack can be mixed on the same
connection.
// client-message — structured messaging over a WebSocket connection
#include <vws/message.h>
#include <stdio.h>
int main(int argc, const char* argv[])
{
// Create connection object
vws_cnx* cnx = vws_cnx_new();
// Connect. This will automatically use SSL if the "wss" scheme is used.
cstr uri = "ws://localhost:8181/websocket";
if (vws_connect(cnx, uri) == false)
{
printf("Failed to connect to the WebSocket server\n");
vws_cnx_free(cnx);
return 1;
}
// Enable tracing
vws.tracelevel = VT_PROTOCOL;
// Create a message
vrtql_msg* request = vrtql_msg_new();
vrtql_msg_set_routing(request, "key", "value");
vrtql_msg_set_header(request, "key", "value");
vrtql_msg_set_content(request, "payload");
// Send (returns < 0 on error)
if (vrtql_msg_send(cnx, request) < 0)
{
printf("Failed to send: %s\n", vws.e.text);
vrtql_msg_free(request);
vws_cnx_free(cnx);
return 1;
}
// Receive (NULL on timeout)
vrtql_msg* reply = vrtql_msg_recv(cnx);
if (reply != NULL)
{
vrtql_msg_free(reply);
}
// Cleanup
vrtql_msg_free(request);
vws_disconnect(cnx);
vws_cnx_free(cnx);
return 0;
}
Server API
The server API is layered and works the same way across media formats: raw
binary data, WebSocket messages, HTTP requests, and VRTQL messages. There are
only a few steps to create a server: you create a server instance, define a
processing function for incoming data, and send data back to the peer.
The model. Every server is created with a *_svr_new(pool_size, backlog, queue_size) call:
pool_size — the number of worker threads in the processing pool.
backlog — the listen() connection backlog; 0 selects the default (128).
queue_size — the maximum request/response queue size; 0 selects the
default (1024).
The three server types are layered on one another: vws_svr (WebSocket) embeds
vws_tcp_svr (core) as its first member, and vrtql_msg_svr (messaging) embeds
vws_svr. Base operations such as vws_tcp_svr_stop() take a vws_tcp_svr*, so
the derived servers are cast with (vws_tcp_svr*) when stopped. The
type-specific calls — vws_svr_run(), vrtql_msg_svr_run(), and so on — take
the derived type directly.
Core server (unstructured data)
The core server (vws_tcp_svr) deals in unstructured blobs. Its processing
callback (vws_tcp_svr_process_data) takes two arguments: the vws_svr_data
instance (a blob of data, allocated on the heap), and the worker thread context.
The worker context is built by the optional worker_ctor, worker_ctor_data
and worker_dtor members. worker_ctor constructs a per-thread context that is
passed into the processing function as its last argument; worker_ctor_data is
user data passed into worker_ctor; and worker_dtor is called on worker
shutdown to clean the context up. These are ideal for per-thread resources such
as dedicated database connections.
// server-core — echo server over the unstructured (TCP) server
#include <vws/server.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
cstr server_host = "127.0.0.1";
int server_port = 8181;
/* Anything you want allocated per worker thread for your processing. */
typedef struct my_env
{
void* thingy;
} my_env;
void process(vws_svr_data* req, void* ctx)
{
vws.trace(VL_INFO, "process (%p)", req);
// Instance of your my_env for this specific worker thread
my_env* env = (my_env*)ctx;
vws_tcp_svr* server = req->server;
//> Prepare the response: echo the data back
// Allocate memory for the data to be sent in response
char* data = (char*)vws.malloc(req->size);
// Copy the request's data to the response data
strncpy(data, req->data, req->size);
// Create response
vws_svr_data* reply = vws_svr_data_own(server, req->cid, (ucstr)data, req->size);
// Free request
vws_svr_data_free(req);
if (vws.tracelevel >= VT_APPLICATION)
{
vws.trace(VL_INFO, "process(cid=%" PRId64 "): %zu bytes",
reply->cid.key, reply->size);
}
// Send reply. This will wake up the network thread.
vws_tcp_svr_send(reply);
}
// Allocate context for a worker thread
void* worker_thread_startup(void* data)
{
my_env* env = (my_env*)malloc(sizeof(my_env));
env->thingy = malloc(1);
return env;
}
// Deallocate context for a worker thread
void worker_thread_shutdown(void* data)
{
my_env* env = (my_env*)data;
free(env->thingy);
free(env);
}
int main(int argc, const char* argv[])
{
// Run server with 10 worker threads, default TCP listen backlog (128) and
// default work queue size (1024 pending requests).
vws_tcp_svr* server = vws_tcp_svr_new(10, 0, 0);
server->on_data_in = process;
vws.tracelevel = VT_THREAD;
// Worker thread context
server->worker_ctor = worker_thread_startup;
server->worker_ctor_data = server;
server->worker_dtor = worker_thread_shutdown;
// Run (blocks until vws_tcp_svr_stop() is called)
vws_tcp_svr_run(server, server_host, server_port);
// Shutdown
vws_tcp_svr_stop(server);
vws_tcp_svr_free(server);
vws_cleanup();
return 0;
}
WebSocket server (delta from core)
Writing a WebSocket server is the same pattern with two changes: create it with
vws_svr_new(), and set the process_ws callback (vws_svr_process_msg), which
operates on WebSocket messages (vws_msg) rather than raw data. (Set your
handler on process_ws, not on_msg_in — the latter is the framework's internal
dispatcher, which calls your process_ws.)