OddSockets C SDK
Official C SDK for OddSockets real-time messaging platform, optimized for embedded systems and IoT devices
Overview & Features
The OddSockets C SDK provides a lightweight, high-performance interface for real-time messaging specifically designed for embedded systems, IoT devices, and resource-constrained environments.
Embedded Optimized
Designed for resource-constrained devices with minimal memory footprint and deterministic behavior.
Memory Efficient
Custom allocators, static allocation options, and careful memory management for embedded use.
Thread Safe
Mutex-protected operations safe for multi-threaded embedded applications and RTOS environments.
Cross Platform
Works on Linux, embedded Linux, FreeRTOS, Arduino, and other POSIX-compatible systems.
Pattern Compliant
100% compliant with JavaScript SDK patterns while optimized for C and embedded constraints.
Real-time Ready
Deterministic memory usage and timing suitable for real-time and safety-critical applications.
Installation
CMake Build
git clone https://github.com/oddsockets/c-sdk.git
cd c-sdk
mkdir build && cd build
cmake ..
make
sudo make install
Dependencies
# Ubuntu/Debian
sudo apt-get install libcurl4-openssl-dev libwebsockets-dev libssl-dev
# CentOS/RHEL
sudo yum install libcurl-devel libwebsockets-devel openssl-devel
# macOS
brew install curl libwebsockets openssl
Manual Installation
# Compile manually
gcc -std=c99 -Wall -Wextra \
-I./src \
-lcurl -lwebsockets -lssl -lcrypto -lpthread \
src/oddsockets.c \
src/manager_discovery.c \
src/websocket_client.c \
src/message_validator.c \
src/json_parser.c \
src/http_client.c \
your_app.c -o your_app
Embedded Integration
For embedded systems, copy these files to your project:
src/oddsockets.h- Main headersrc/oddsockets.c- Main implementationsrc/manager_discovery.c- Manager discoverysrc/websocket_client.c- WebSocket clientsrc/message_validator.c- Message validationsrc/json_parser.c- JSON parsingsrc/http_client.c- HTTP client
# Cross-compile for ARM
cmake -DCMAKE_TOOLCHAIN_FILE=cmake/arm-linux-gnueabihf.cmake ..
make
Quick Start
Basic Usage
#include "oddsockets.h"
int main() {
// Initialize configuration
oddsockets_config_t config;
oddsockets_config_init(&config, "ak_live_1234567890abcdef");
// Create client
oddsockets_client_t* client = oddsockets_create(&config);
// Connect to platform
oddsockets_connect(client);
// Create and subscribe to channel
oddsockets_channel_t* channel = oddsockets_channel_create(client, "my-channel");
oddsockets_channel_subscribe(channel, on_message, NULL, NULL);
// Publish a message
oddsockets_channel_publish(channel, "Hello from C SDK!", NULL);
// Event loop
while (oddsockets_get_state(client) == ODDSOCKETS_STATE_CONNECTED) {
oddsockets_process_events(client);
usleep(10000); // 10ms
}
// Cleanup
oddsockets_channel_destroy(channel);
oddsockets_destroy(client);
return 0;
}
Message Callback
void on_message(const char* channel_name, const char* message, void* user_data) {
printf("Received on %s: %s\n", channel_name, message);
}
void on_connection_state(oddsockets_state_t state, void* user_data) {
printf("Connection state: %s\n", oddsockets_state_string(state));
}
void on_error(oddsockets_error_t error, const char* message, void* user_data) {
printf("Error %d: %s\n", error, message);
}
Configuration
Client Configuration
oddsockets_config_t config;
oddsockets_config_init(&config, "your-api-key");
// Optional configuration
strncpy(config.user_id, "user123", sizeof(config.user_id) - 1);
config.auto_connect = true;
config.reconnect_attempts = 5;
config.reconnect_delay_ms = 1000;
config.connection_timeout_ms = 10000;
config.enable_ssl = true;
config.ssl_verify_peer = true;
// Callbacks
config.connection_callback = on_connection_state;
config.error_callback = on_error;
config.log_callback = on_log;
config.log_level = ODDSOCKETS_LOG_INFO;
Channel Options
// Subscribe options
oddsockets_subscribe_options_t subscribe_options = {
.max_history = 100,
.retain_history = true,
.enable_presence = false
};
oddsockets_channel_subscribe(channel, callback, user_data, &subscribe_options);
// Publish options
oddsockets_publish_options_t publish_options = {
.ttl_seconds = 3600,
.metadata = "priority:high",
.store_in_history = true
};
oddsockets_channel_publish(channel, message, &publish_options);
Examples
Explore comprehensive examples demonstrating the OddSockets C SDK in embedded and IoT environments:
Embedded Systems Integration
FreeRTOS Integration
#include "FreeRTOS.h"
#include "task.h"
#include "oddsockets.h"
void oddsockets_task(void *pvParameters) {
oddsockets_config_t config;
oddsockets_config_init(&config, "your-api-key");
// Use FreeRTOS heap
oddsockets_set_memory_functions(pvPortMalloc, vPortFree, pvPortRealloc);
oddsockets_client_t* client = oddsockets_create(&config);
while (1) {
oddsockets_process_events(client);
vTaskDelay(pdMS_TO_TICKS(10)); // 10ms delay
}
}
void app_main() {
xTaskCreate(oddsockets_task, "oddsockets", 4096, NULL, 5, NULL);
}
Arduino/ESP32 Integration
#include
#include "oddsockets.h"
const char* ssid = "your-wifi";
const char* password = "your-password";
oddsockets_client_t* client;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Initialize OddSockets
oddsockets_config_t config;
oddsockets_config_init(&config, "your-api-key");
client = oddsockets_create(&config);
oddsockets_connect(client);
}
void loop() {
oddsockets_process_events(client);
delay(10);
}
Static Memory Allocation
// Enable static allocation at compile time
#define ODDSOCKETS_ENABLE_STATIC_ALLOCATION
// Pre-allocated memory pools
static uint8_t client_memory[ODDSOCKETS_CLIENT_SIZE];
static uint8_t channel_memory[ODDSOCKETS_MAX_CHANNELS][ODDSOCKETS_CHANNEL_SIZE];
// Custom allocator for deterministic memory usage
void* embedded_malloc(size_t size) {
// Your custom allocation logic
return get_from_memory_pool(size);
}
void embedded_free(void* ptr) {
// Your custom deallocation logic
return_to_memory_pool(ptr);
}
int main() {
// Set custom memory functions
oddsockets_set_memory_functions(embedded_malloc, embedded_free, embedded_realloc);
// Rest of your application
}
Performance & Compatibility
OddSockets C SDK delivers exceptional performance optimized for embedded and resource-constrained environments:
Platform Support
- Linux (x86, ARM, MIPS)
- Embedded Linux
- FreeRTOS
- Arduino/ESP32
- Zephyr RTOS
- Custom RTOS
Compiler Support
- GCC 7+ (C99)
- Clang 8+
- ARM GCC
- ESP-IDF
- Cross-compilation ready
Memory Optimization Features
Custom Allocators
Pluggable memory management for specialized embedded memory systems.
Static Allocation
Compile-time memory allocation for deterministic real-time systems.
Memory Pools
Pre-allocated memory pools to avoid fragmentation in long-running systems.
Memory Tracking
Built-in memory usage tracking and leak detection for development.