WebSockets represent a critical protocol for modern IoT applications, enabling fast, efficient, real-time, bi-directional communication between devices and clients. This technology moves beyond traditional request-response paradigms of HTTP by maintaining persistent connections, dramatically improving communication efficiency, reducing latency, and simplifying real-time application architectures.
WebSockets is a network communication protocol designed to facilitate full-duplex, real-time communication between a client and server over a single, long-lived TCP connection. Unlike traditional HTTP, where each interaction typically involves a separate request and response, WebSockets maintain a continuous connection, allowing instantaneous data transfer in both directions. This makes WebSockets particularly suitable for interactive IoT applications, live dashboards, real-time device monitoring, and immediate control scenarios. Conveniently, WebSocket connections can be established over normal HTTP connections and that works effectively with firewalls and existing network infrastructure.
WebSockets are ideal for scenarios requiring low latency and real-time interactions, such as:
Embedthis Ioto provides comprehensive support for WebSockets, enabling easy integration for both client-side and server-side scenarios. Ioto’s WebSockets support includes:
Ioto can act as a WebSockets server, seamlessly integrating with its embedded web server component.
To respond to WebSocket requests, implement an Action handler using Ioto’s C API. The Action handler is invoked when a request is received that matches the given URL path.
webAddAction(host, "/chat", chatHandler);
static void chatHandler(Web *web) {
if ((rc = webRunSocket(web, onRead)) <= 0) {
// Error < 0, Closed == 0
}
}
The action routine is invoked once at the start of the request if the WebSocket connection is valid.
Your action routine must call webRunSocket
to process WebSocket messages. The webRunSocket call will process WebSocket messages and will block until the connection is closed. Other fibers continue to run.
When an incoming WebSocket message is received, your onRead
callback will be invoked. This is where you can do your custom processing.
static void onRead(WebSocket *ws, cchar *buf, ssize len)
{
printf("Received message %s\n", buf);
}
To send a message, use the webSocketSend
API:
void onRead(WebSocket *ws, char *buf, ssize len, void *arg)
{
webSocketSend(ws, "%s", "Response message");
}
To start a WebSocket client session, use the urlWebSocket API.
int status = urlWebSocket("wss://my.com/post", onRead, NULL, NULL);
This API will block and service incoming and outgoing WebSocket messages until the connection is closed. While blocked, other Fibers will continue to run. The return status will be zero for an orderly close and a negative status for an abortive close.
The urlWebSocket API takes a callback function onRead
argument that is invoked to receive incoming WebSocket messages.
void onRead(WebSocket *ws, char *buf, ssize len, void *arg)
{
printf("Received %s\n", buf);
webSocketSend(ws, "%s", "Response message");
}
To send WebSocket messages, use webSocketSend.
Ioto also provides a HTTP client test app called url
. This can be used to issue test WebSocket requesets.
To initiate a WebSocket connection as a client:
url ws://localhost/chat
This opens a WebSocket connection to a WebSockets-enabled endpoint. You can then receive messages over this channel.
To send WebSockets test messages, use:
url --webSocketSize 1k ws://localhost/chat
Read about the Url Command in the man page.
WebSockets by default validates all messages to ensure the transmission of only valid UTF-8 strings. If you control both ends of the WebSocket communication channel, you can skip UTF validation and significantly optimize communications by setting the validateUTF
option to false.
{
web: {
webSockets: {
validateUTF: false
}
}
}
Embedthis Ioto provides efficient WebSocket support, enabling developers to build responsive, real-time embedded IoT applications. With Ioto’s streamlined architecture, integrating real-time functionality becomes straightforward, simplifying development tasks and improving device apps and the end-user experience.
{{comment.name}} said ...
{{comment.message}}