WebSocket Library
July 21, 2026 · View on GitHub
1. Introduction
The WebSocket Library is a comprehensive, enterprise-grade implementation designed specifically for ModusToolbox applications, providing robust WebSocket communication capabilities for Infineon embedded systems. This library enables developers to implement RFC 6455 compliant WebSocket clients and servers with full-duplex, real-time communication support across a wide range of Infineon microcontroller platforms.
Built with reliability and performance in mind, the library offers thread-safe operations, advanced connection management, and sophisticated event-driven architecture. It seamlessly integrates with ModusToolbox ecosystem components including FreeRTOS, Microsoft ThreadX, lwIP networking stack, NetXSecure stack, and mbedTLS security framework to deliver production-ready WebSocket functionality for IoT, industrial automation, and connected device applications.
2. Features
- Dual-Role Architecture: Complete client and server implementations with role-specific optimizations
- Enterprise Security: Full TLS/SSL support with certificate-based authentication and secure connection establishment
- Multi-RTOS Support: Compatible with both FreeRTOS and Microsoft ThreadX real-time operating systems
- Flexible Network Stacks: Support for lwIP and NetXSecure networking implementations
- Scalable Server Design: Multi-client concurrent connection handling with configurable resource management
- Real-Time Communication: Low-latency bidirectional messaging with automatic fragmentation and reassembly
- Production-Ready Reliability: Comprehensive error handling, connection recovery, and resource cleanup
- Event-Driven Programming Model: Asynchronous notification system for responsive application development
3. Architecture and Design Principles
-
Modular Design : The WebSocket Library uses a modular design that is easy to maintain, scale, and reuse. The design follows good software practices like separating different concerns and using clean interfaces.
-
Complete Feature Set: The WebSocket Library provides a full implementation of the RFC 6455 WebSocket protocol with all the features needed for both client and server applications.
-
Event-Based System: The library uses an event notification system that tells your application what's happening.
3.1 Client-Only Events:
MTB_WEBSOCKET_EVENT_CONNECTED- Connection successfully established to external serverMTB_WEBSOCKET_EVENT_CLOSE- Connection gracefully closed by either client or server
3.2 Server-Only Events:
MTB_WEBSOCKET_EVENT_SERVER_STARTED- Server successfully started and actively listening for connectionsMTB_WEBSOCKET_EVENT_CLIENT_ACCEPTED- Server accepted new client connection after successful handshakeMTB_WEBSOCKET_EVENT_BROADCAST_COMPLETE- Mass broadcasting operation completed to all or selected clientsMTB_WEBSOCKET_EVENT_CLIENT_DISCONNECTED- Existing client disconnected from serverMTB_WEBSOCKET_EVENT_SERVER_STOPPED- Server stopped listening for new connections
3.3 Common Events (Client and Server):
MTB_WEBSOCKET_EVENT_MESSAGE- Data received from server/clients containing text or binary payloadMTB_WEBSOCKET_EVENT_PING- Ping control frame received requiring pong responseMTB_WEBSOCKET_EVENT_PONG- Pong control frame received in response to sent pingMTB_WEBSOCKET_EVENT_ERROR- Critical error occurred during WebSocket operationMTB_WEBSOCKET_EVENT_STATE_CHANGE- WebSocket connection state transitioned between operational phases
4. Core Configuration Parameters
Configure these parameters by defining them in your application's Makefile or by copying and modifying the configuration file (see step 7 in Quick Start Guide).
Instance Limits:
MTB_MAX_NUM_OF_WEBSOCKET_CLIENTS- Maximum number of client instancesMTB_WEBSOCKET_MAX_CLIENTS_PER_SERVER- Maximum connections per server instance
WebSocket Protocol Configuration:
MTB_WEBSOCKET_MAX_FRAME_SIZE- Maximum WebSocket frame size for security and memory managementMTB_WEBSOCKET_MAX_CHUNK_SIZE- Maximum frame chunk size during reception
Timeout Configuration:
MTB_WEBSOCKET_MESSAGE_SEND_TIMEOUT_MS- WebSocket message send timeoutMTB_WEBSOCKET_MESSAGE_RECEIVE_TIMEOUT_MS- WebSocket message receive timeout for partial data
Event System Configuration:
MTB_WEBSOCKET_EVENT_QUEUE_SIZE- Event queue size for WebSocket eventsMTB_WEBSOCKET_EVENT_THREAD_PRIORITY- Event processing thread priority
Threading Configuration:
MTB_WEBSOCKET_EVENT_THREAD_STACK_SIZE- Event processing thread stack size
5. Supported Hardware Platforms
The WebSocket Library is optimized and validated for the following Infineon evaluation and development platforms, providing comprehensive support for advanced wireless connectivity and edge computing applications:
- CYW955913EVK-01 Wi-Fi Bluetooth® Prototyping Kit (CYW955913EVK-01)
- PSOC™ Edge E84 Evaluation Kit
6. Dependencies
The WebSocket Library uses the WSLAY library, a publicly available WebSocket protocol implementation that provides RFC 6455 compliant frame parsing and message handling. The library integrates with ModusToolbox and supports multiple RTOS and networking stack combinations.
7. Quick Start Guide
This comprehensive guide provides step-by-step instructions for integrating the WebSocket Library into your ModusToolbox project and configuring it for optimal performance with your chosen RTOS and networking stack.
-
To use websocket library with Wi-Fi kits on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull websocket library and wifi-core-freertos-lwip-mbedtls library which will internally pull secure-sockets, wifi-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull wifi-core-freertos-lwip-mbedtls and websocket libraries create the following .mtb files in deps folder.
-
To use websocket library with CYW955913EVK-01 kit, the application should pull websocket library and wifi-core-threadx-cat5 library which will internally pull secure-sockets, wifi-connection-manager and other dependent modules. To pull wifi-core-threadx-cat5 and websocket libraries create the following .mtb files in deps folder.
-
Review and make the required changes to the pre-defined configuration files.
- The configuration files are bundled with the wifi-mw-core library for FreeRTOS, lwIP, and Mbed TLS. See README.md for details.
- If the application is using bundle library then the configuration files are in the bundle library. For example if the application is using Wi-Fi core freertos lwip mbedtls bundle library, the configuration files are in
wifi-core-freertos-lwip-mbedtls/configsfolder. Note: Configuration file changes are not required for CYW955913EVK-01.
-
Define the following COMPONENTS in the application's Makefile for FreeRTOS based platforms.
COMPONENTS=FREERTOS MBEDTLS LWIP SECURE_SOCKETS -
Define the following COMPONENTS in the application's Makefile for ThreadX based platforms.
COMPONENTS=SECURE_SOCKETS -
By default, the websocket Library disables all the debug log messages. To enable log messages, the application must perform the following:
-
Add the
ENABLE_WEBSOCKET_LOGSmacro to the DEFINES in the code example's Makefile. The Makefile entry would look like as follows:DEFINES+=ENABLE_WEBSOCKET_LOGS -
Call the
cy_log_init()function provided by the cy-log module. cy-log is part of the connectivity-utilities library.See connectivity-utilities library API documentation for cy-log details.
-
-
The WebSocket library provides a configuration file to customize behavior and performance parameters:
- Copy the configuration file from the library to your project:
cp mtb_shared/websocket/configs/mtb_websocket_config.h ./- Adjust configuration parameters as needed for your application:
// mtb_websocket_config.h - Key configuration options #define MTB_MAX_NUM_OF_WEBSOCKET_CLIENTS 2 #define MTB_WEBSOCKET_MAX_CLIENTS_PER_SERVER 5 #define MTB_WEBSOCKET_EVENT_QUEUE_SIZE 50 #define MTB_WEBSOCKET_MAX_FRAME_SIZE 65536 #define MTB_WEBSOCKET_MAX_CHUNK_SIZE 8192 #define MTB_WEBSOCKET_MESSAGE_SEND_TIMEOUT_MS 1000 #define MTB_WEBSOCKET_MESSAGE_RECEIVE_TIMEOUT_MS 500 -
The application utilizing the WebSocket library, which leverages the secure-sockets library for socket operations and runs on the CM55 core of the PSoC™ Edge E84 kits, requires 64KB of non-cacheable memory per TLS connection. By default, the secure-sockets supports a single TLS connection. To enable support for multiple TLS connections, the following modifications are necessary:
- To support TLS connections, a minimum of 64KB of non-cacheable memory is required per connection. To enable support for multiple TLS connections, it is necessary to define the CYCFG_MBEDTLS_BUFFER_SIZE macro in the Makefile. Specifically, the macro should be set to a value that is a multiple of 64KB, where the multiplier corresponds to the desired number of concurrent TLS connections. For instance, to support five TLS connections, the macro should be defined with a value of 320KB (5 x 64KB). The corresponding Makefile entry would be:
DEFINES+=CYCFG_MBEDTLS_BUFFER_SIZE=320*1024