Introduction
This document discusses how to communicate with a server using WebSocket or HTTP protocol. This kind of communication is usually done by web browsers with sites on the internet.
HTTP commands can also control Aximmetry, it is discussed in a separate document here: External Control of Aximmetry via HTTP
WebSocket or HTTP, What is the Difference?
WebSocket is characterized by its low latency and bidirectional communication protocol, allowing data to be transmitted in both directions simultaneously. The connection is established only once, enabling efficient real-time interaction between clients and servers.
In contrast, HTTP operates with a unidirectional communication style. With every request, a new connection needs to be established, leading to increased overhead due to additional headers and metadata. HTTP is well-suited for fetching resources and data from servers in a request-response fashion like in RESTful APIs.
Websocket Client
Input Pins
- Connect
Connects to the target server. It must be turned on to send data.
When the module is successfully connected to the server the Connected output pin turns True. - URL
The URL web address of the server, which consists of a protocol prefix and a domain name. Optionally, additional elements such as a port, path, and other suffixes may follow the domain name.
A WebSocket URL's protocol prefix will likely be "ws://" instead of "http://", such as "ws://example.com/socket", where "ws" indicates that the connection should be made using the WebSocket protocol. Keep in mind that this does not imply that URLs with "http://" prefix don't work as WebSockets.
Also, the module supports secure connection, which can be "wss://" where "s" indicates a secure (encrypted) connection, similar to "https://".
NOTE: You can use the URL Compose and URL Decompose modules to easily put together URL addresses or to parse them. - Socket IO Mode
Turn on Socket.IO mode when the targeted server uses the Socket.IO library for communication.
When defining the text for the Data pin, it should be formatted as follows: ["eventname", mydata]
Where the mydata part can be in JSON format. For example: ["message", {"something": "Text", "otherthing": 4.35}].
NOTE: Socket.IO is a JavaScript library that adds several features and enhancements on top of WebSocket communication.
NOTE: Socket.IO does not support Binary mode; it only operates using Socket.IO's WebSocket protocol.
NOTE: Aximmetry's collection data type can be easily converted to JSON using the Collection To JSON module. Subsequently, it can be parsed into the text ["eventname", ] by using the Text Concat Multi module. - Binary Mode
Switches between sending text or byte (binary) data. - Data and Data Bytes
The text or bytes of the message that will be sent to the target server. To use Byte data instead of Text, turn on the Binary Mode pin. - Auto Send and Send
Sending of the message can be performed in two ways. You can send it at a specific time by triggering it via the Send pin. Alternatively, you can turn On Auto Send, in this case, each time the Data value is changed it is sent automatically.
Output Pins
- Received and Received Bytes
The data that is received from the server. To receive Byte data instead of Text, turn on the Binary Mode pin.
When receiving byte data, these modules can convert the data: Bytes To Text, Bytes To Scalar, Bytes To Vector, and Bytes To Integer. - Connected
The Connected output pin is True when the module has established the connection with the target server.