WebSocket
Enable real-time, bidirectional communication for embedded systems
- Exchange data over persistent TCP connections
- Integrate seamlessly with existing web servers
- Minimize memory usage with a lightweight implementation
Overview
The emNet WebSocket add-on enables bidirectional communication between embedded systems and connected clients over a persistent TCP connection. Based on the WebSocket protocol, it allows data to be exchanged without establishing a new HTTP connection for every message.
Its lightweight implementation makes it suitable for resource-constrained embedded systems, including Machine-to-Machine (M2M) and Internet of Things (IoT) applications. The add-on can be used with emNet's web server or independently with another TCP/IP stack that provides a compatible socket API.
Key features
- Converts a synchronous HTTP connection into an asynchronous data connection
- Can traverse firewalls using the well known HTTP port
- Can be used with or without web server
- Seamless integration with emWeb web server
- Low memory footprint
- Optimized API for embedded systems
- Independent of the TCP/IP stack: Any stack with sockets can be used
- Project for executable on PC for Microsoft Visual Studio included
WebSocket backgrounds
The WebSocket protocol is an IoT (Internet of Things) protocol that allows to upgrade a regular synchronous HTTP connection into an asynchronous bidirectional data tunnel. It allows to establish a regular HTTP connection with a webserver, checking if both sides, client and server, understand the WebSocket protocol depending on the answer sent back by the webserver.
The non intrusive test for checking the WebSocket protocol being supported is compatible with the HTTP/1.1 standard and will return a page with an error code in worst case if the protocol is not supported by the webserver. The webserver service for serving regular pages remains fully operational while coexisting with the WebSocket protocol.
The WebSocket protocol is using the TCP/IP protocol for data, using the HTTP/1.1 protocol only to initially establish a WebSocket connection.
WebSocket | Application layer protocol: VNC / MQTT / ... |
|---|---|
| Application layer | Application layer protocoll: Websocket |
| Transport layer | Transport layer protocol: TCP |
| Network layer | Link layer protocol: IPv4 / IPv6 |
| Link layer | Link layer protocol: Ethernet, WiFi |
An advantage of the WebSocket protocol is that it establishes a connection through the HTTP protocol. This allows an easier handling in terms of firewalls as typically the standard port TCP 80 is allowed for outgoing connections, allowing WebSocket clients to work without problems. For a WebSocket server, offering webpages and WebSocket tunneled protocols only a single port is required to be opened in the firewall or allowed to be forwarded by a router from the Internet to your local network.
Resource Usage
The ROM usage depends on the compiler options, the compiler version and the used CPU. The memory requirements of the WebSocket add-on presented in the tables below have been measured on a Cortex-M4 system. Details about the further configuration can be found in the sections of the specific example.
The following resource usage has been measured on a Cortex-M4 system using SEGGER Embedded Studio V3.10e, size optimization.
Relevant parts
| Document | Download |
|---|---|
| [RFC 6455] | Direct link: The WebSocket Protocol |
Requirements
TCP/IP stack: The emNet WebSocket add-on requires a TCP/IP stack. It is optimized for emNet, but any RFC-compliant TCP/IP stack that has a socket API can be used. The shipment includes a Win32 simulation, which uses the standard Winsock API as well as an implementation which uses the socket API of emNet.
How it works
Application data sent using the WebSocket protocol can be described as UDP like framing of data within a TCP data stream. While this might sound complex at first, the protocol itself is not.
The main purpose of the WebSocket protocol is to allow a device with limited resources sending one big block of data in chunks (called a frames in the WebSocket protocol) whenever data is ready/generated instead of collecting the data before sending. At the same time control commands should be able to be processed anytime between frames. At the same time the TCP protocol ensures that data is not lost and frames will be sent and received in the correct order.
The WebSocket protocol achieves these goals by sending all data in frames that have a header upfront. The encapsulation of the protocol is shown below:
Data framing
The WebSocket protocol combines the following properties of TCP and UDP:
TCP properties
- Lost data is retransmitted.
- Data is received in the correct order.
- The connection status can be checked by using TCP KEEPALIVES.
UDP properties
- Data can be sent in small frames (while being part of a larger message).
- The length of the frame is known (the complete message length is not).
- Control frames (UDP: other data) can be sent between data frames.
