Skip to content

Using C# with Coreflux

Coreflux provides a managed MQTT client for C# developers, making it easier to integrate your applications with the platform.

Installation

To use the Coreflux MQTT Managed client, you need to reference the appropriate namespace in your project:

using Coreflux.API.Networking.MQTT;

Starting the Client

You can start the MQTT client by specifying the server's IP or DNS:

MQTTController.Start("127.0.0.1", 1883); // Using IP with normal TCP/IP socket
MQTTController.Start("cloud.coreflux.org", 1883); // Using DNS with normal TCP/IP socket
MQTTController.Start("cloud.coreflux.org:8080/mqtt", 8080, "", "", false, true); // Using DNS, without username, no password, no TLS, and with WebSocket

Event Handling

To handle various MQTT events, you can connect to the provided event handlers:

MQTTController.OnConnect += YourConnectHandler;
MQTTController.NewPayload += YourPayloadHandler;
MQTTController.OnDisconnect += YourDisconnectHandler;

Subscribing to Topics

To subscribe to a topic and retrieve its payload:

string payload = MQTTController.GetData("your/topic");

Publishing to Topics

To publish data to a topic:

MQTTController.SetData("your/topic", "your payload", 0, false);

Anomaly Detection with C

Detecting unauthorized connections is crucial for maintaining the security of your IoT ecosystem. Here's a simple example using C# to monitor remote connections and compare them against a list of authorized endpoints:

using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Coreflux.API.Networking.MQTT;

public class ConnectionMonitor
{
    private readonly string[] authorizedEndpoints;

    public ConnectionMonitor()
    {
        // Load authorized endpoints from a file
        authorizedEndpoints = File.ReadAllLines("authorized_endpoints.txt");

        // Subscribe to the MQTT topic for active connections
        MQTTController.Start("127.0.0.1");
        MQTTController.NewPayload += CheckAuthorizedConnection;
    }

    private void CheckAuthorizedConnection(MQTTNewPayload payload)
    {
        if (payload.topic == "$SYS/{DetectedOS}/Comms/TCP/ActiveConnections")
        {
            var connections = JArray.Parse(payload.payload);
            foreach (var connection in connections)
            {
                var remoteEndpoint = connection["RemoteEndPoint"].ToString().Split(':')[0]; // Extract IP address
                if (!authorizedEndpoints.Contains(remoteEndpoint))
                {
                    Console.WriteLine($"Unauthorized connection detected from IP: {remoteEndpoint}");
                    // Take further action, e.g., alert, log, block, etc.
                }
            }
        }
    }
}

In this example, the ConnectionMonitor class loads a list of authorized endpoints from a file and subscribes to the MQTT topic that provides active connections. When a new payload is received, it checks each connection against the list of authorized endpoints. If an unauthorized connection is detected, it logs a warning.