Skip to content

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.

Prerequisites

  • Ensure you have the coreflux_ws_broker asset 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:

-L | --login <amazingUser> <lamePassword>
Use this command to log in to the Coreflux cloud using the provided username and password.

List All Assets:

-l | --list
Once logged in, you can list all available assets using this command. Look for the coreflux_ws_broker asset in the list.

Install the Asset:

-I | --install coreflux_ws_broker
After logging in and listing the assets, you can install the coreflux_ws_broker asset using its name.

Configuration

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:

{
  ...
  "CertificatePath": "/path/to/your/certificate.pfx",  // Use the appropriate path based on your OS
  "CertificatePassword": "YourCertificatePassword",
  ...
}

Update the Broker Configuration

After editing the configuration JSON, you need to send the updated configuration to the broker. Use a 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.

Important Notice

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

Once you've configured WebSockets, you can test the connection using any MQTT client that supports MQTT over WebSockets. Here's a basic example using the JavaScript Paho client:

const client = new Paho.MQTT.Client("your_broker_address", websocket_port, "/mqtt", "clientId");
client.connect({
    onSuccess: () => {
        console.log("Connected to MQTT over WebSockets!");
    }
});

Replace your_broker_address and websocket_port with your broker's address and the WebSocket port you've configured, respectively.

Conclusion

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.