Team XSockets.NET

QoS - Client Sample

This section contains basic samples showing QoS level 1 and the retained flag.

We try to use Putty in as many samples as possible to show that you can connect anything with TCP/IP.

Supported Clients

The clients (created by Team XSockets) that supports QoS is the C# clients. This means that QoS is supported in:

  • Xamarin Android
  • Xamarin iOS
  • Windows Store
  • Windows Phone
  • All C# (.NET 4.5+) projects

Currently we have no plans for adding QoS to the other clients, but let us know if you have special requirements.

QoS Level 1

In this sample we will mix Putty and the C# client to take a look at how QoS level 1 works. This means that the client will re-send the message as long as the server does not acknowledge the message.

To see this in action we add a message-interceptor to see messages going in/out of the server.

Message Interceptor

This is just to be able to see the message-flow

public class MyMessageInterceptor : IMessageInterceptor
{
    public void OnIncomingMessage(IXSocketProtocol protocol, IMessage message)
    {
        Composable.GetExport<IXLogger>().Information("In: {@m}", message);
    }

    public void OnOutgoingMessage(IXSocketProtocol protocol, IMessage message)
    {
        Composable.GetExport<IXLogger>().Information("Out: {@m}", message);
    }
}

Test

Now when we send in a message with QoS Level 1 to the server we expect the server to acknowledge that message. So we should see a message going in with QoS = 1 and then the server should respond with a MsgAck to a message with the same ID.

Client Code

The sample will connect to the generic controller, so we will not need to write any server-side code.

// Create a client
var client = new XSockets.XSocketClient("ws://localhost:4502", "http://localhost", "generic");
// Extract the generic controller
var g = client.Controller("generic");
// When controller is openened
g.OnOpen += (s, e) =>
{
    Console.WriteLine("Generic Open, sending temp value with QoS 1");
    g.Invoke("temp", 23.5, QoS.AtLeastOnce);                    
};
// Open connection
await client.Open();

Server Log Output

In the image below you can see that the server first got an incoming message with QoS set to AtLeastOnce. The server then sent an acknowledge back to the client for the message so that the client knows that it was delivered. At the last line we see the server sending the actual message out since we used the generic controller.

QoS Level 1 Demo

Retain

If we send in a message with the retain flag the server will store the message as a last known good message for the topic. Later on when a client subscribes to the topic the server will publish the retained message(s) that matches the topic for the subscription.

Note that if the subscription matches several retained topics they will all be sent back to the client.

Test

We send in a message with the retain flag set to true. Then another client (Putty) will open a connection and subscribe to the topic. Putty will then get the last known value published back instantly.

Client Code

The client code is almost identical to the one above. The only difference is that we pass in true for the retain parameter. You can also see that we now specify a hierarchy for the topic home/basement/temp since we can use wildcards in subscriptions.

var client = new XSockets.XSocketClient("ws://localhost:4502", "http://localhost", "generic");                
var g = client.Controller("generic");
g.OnOpen += (s, e) =>
{
    Console.WriteLine("Generic Open, sending temp value with QoS 1 and retain flag enabled");
    g.Invoke("home/basement/temp", 23.5, QoS.AtLeastOnce, true);                    
};
await client.Open();

Putty

As so many times before in this book we can now use Putty to show the functionality. So first of all we ran the client code above. Now we will connect putty and subscribe to the topic home/+/temp. Since the client code above had the retain flag set to true for this topic we will get the last value back instantly.

QoS Retain Putty

As you can see above XSockets supports topic hierarchies (same as MQTT) as of 5.0.3. We can subscribe to home/+/temp and get the value back. This works since + replaces a single level. In this case basement.

So even though we connected Putty after the sending the message from C#, the server could return the last know value thanks to the retain flag.

results matching ""

    No results matching ""