Setting up WebSockets with Coreflux
WebSockets provide a full-duplex communication channel over a single, long-lived connection, making them ideal for real-time data transfer scenarios such as those required by IoT devices. In the context of MQTT, WebSockets allow MQTT messages to be sent over a WebSocket connection, making it possible to connect to the MQTT broker directly from web browsers and other platforms that support WebSockets.
Coreflux's MQTT Broker supports MQTT over WebSockets, allowing seamless integration with web applications and other platforms. This guide will walk you through the process of setting up and configuring WebSockets for your Coreflux MQTT Broker.
- Ensure you have the
coreflux_ws_brokerasset power-up purchased and installed. This is a premium feature of our broker.
Buy Web Sockets Asset first
The WebSockets feature is only available if the
coreflux_ws_broker (via command list) asset power-up is purchased. It is a premium feature of our broker. Ensure you have acquired this asset before attempting to set up WebSockets.
Installing the Websocket Asset
Login to Coreflux Cloud:
List All Assets:
coreflux_ws_broker asset in the list.
Install the Asset:
coreflux_ws_broker asset using its name.
Before you can use WebSockets, you need to ensure that the broker is correctly configured to handle WebSocket connections.
Edit the Configuration JSON
If you haven't already changed the Configuration, you can now edit your existing configuration JSON to include the WebSocket parameters:
"WebsocketPort": 5000, // The port for MQTT communication over WebSockets
"WebsocketPortTls": 443, // The port for MQTT communication over WebSockets with TLS (encrypted)
"BindIpForWebSockets": "0.0.0.0", // The IP address the MQTT broker binds to for MQTT communication over WebSockets
"BindIpForWebSocketsTls": "0.0.0.0", // The IP address the MQTT broker binds to for MQTT communication over WebSockets with TLS
(Optional) Secure WebSockets with TLS
If you intend to use WebSockets over TLS (WSS), you'll need to specify the path to your certificate and its password in the configuration:
Update the Broker Configuration
After editing the configuration JSON, you need to send the updated configuration to the broker. Use an MQTT client to publish the complete configuration JSON to the
$SYS/Coreflux/Config/New topic. This will ensure that the broker updates its configuration based on the provided JSON.
Always ensure to send the full configuration to the
$SYS/Coreflux/Config/New topic. Partial configurations might lead to a FAIL feedback in the configuration or bad behaviors.
Testing the WebSocket Connection
websocket_port with your broker's address and the WebSocket port you've configured, respectively.
Setting up MQTT over WebSockets with Coreflux enhances the versatility of your IoT infrastructure, allowing for real-time communication directly from web browsers and other platforms. By following this guide, you'll ensure a secure, efficient, and persistent communication channel for your IoT devices and applications.