<commit_before># MQTT
A MQTT protocol plugin for XSockets based on [<NAME>](https://twitter.com/ppatierno)s work with [GnatMQ](https://mqttbroker.codeplex.com/) / [M2MQTT](https://m2mqtt.wordpress.com/)

##Test MQTT in XSockets.NET

NOTE: Currently release on [nuget](https://www.nuget.org/packages/XSockets/5.0.0-beta4) as a pre-release

Simplest way is to just create a .NET 4.5 Console Application in C#

1. Create a new Console Application in C# .NET 4.5
2. `Install-Package XSockets -pre`
3. Add start start up code for the XSockets server
        
        //using XSockets.Core.Common.Socket;
        //using XSockets.Plugin.Framework;
        using (var container = Composable.GetExport<IXSocketServerContainer>())
        {
            container.Start();
            Console.ReadLine();
        }
        
4. Download [MQTT FX](http://mqttfx.jfx4ee.org/index.php/download)
5. Open `MQTT FX` and set port to be `4502` (if you did not change this in XSockets to be something else)
        
        //Add this class in the project if you want port 1883 instead
        //Note that you can add as many configuration classes as you like to get more endpoints.
        using XSockets.Core.Configuration;
        public class MqttConfig : ConfigurationSetting
        {
            public MqttConfig():base("ws://localhost:1883"){}
        }
        
6. Connect and test MQTT

## About The Implementation
The goal was to leave Paolo's implementation as intact as possible. So under source you can see a `GnatMQ` folder, that folder contains the code from GnatMQ at CodePlex. 

The only changes made was:
 - Added a static `Parse` method to the `MqttMsgConnect` class
 - The `OnMqttMsgConnected` is currently made internal instead of private. Hopefulle we can get back to private here in the end.
 
The XSockets plugins created are found under the `Modules`folder:
 - `MqttController`is the XSockets controller that will receive Mqtt messages. This module has a MqttClient instance
 - `MqttManager` is a internal XSockets controller (singleton) that will be used instead of the default MqttBroker since XSockets has its own transport layer.
 - `MqttProtocol` the custom protocol that make sure that the MQTT clients will use the MQTT logic
  
 
## Anything To MQTT
**NOTE: This feature is not yet published to Nuget**  

Since you can connect anyhting that has TCP/IP to XSockets you can send a message to the MQTT clients from anything that has TCP/IP.

Below is a simple controller `Foo` that will accept any message to the method `Bar` and send that message to all subscribing MQTT clients.
    
    public class Foo : XSocketController
    {        
        public void Bar(IMessage message)
        {
            //Send to all clients MQTT excluded
            this.InvokeToAll(message);
            //Send to all MQTT clients, still just a POC, so it is kind of hacky
            foreach (var c in this.FindOn<MqttController>())
            {
                c.MqttClient.Publish(message.Topic,Encoding.UTF8.GetBytes(message.Data));
            }            
        }
    }
    
###To Test This...
First of all add the sample class `Foo` from above to your XSockets sample project.

1. Start XSockets server
2. Connect a MQTT client (MQTT FX)
3. Subscribe to `bar`
4. Open up Putty
5. Connect to the server (probably localhost and port 4502). Remember to choose `Raw` as connection type
6. Type in `PuttyProtocol` in putty and hit enter
7. Then type `foo|bar|Hello from putty` and hit enter

Your MQTT client should now get the message `Hello from putty` on the topic `bar` 
<commit_msg>updated readme to new documentation<commit_after># MQTT
A MQTT protocol plugin for XSockets based on [<NAME>](https://twitter.com/ppatierno)s work with [GnatMQ](https://mqttbroker.codeplex.com/) / [M2MQTT](https://m2mqtt.wordpress.com/)

## Documentation

See: [XSockets on GitBook](https://uffebjorklund.gitbooks.io/xsockets-net-5/content/mqtt/mqtt.html)
{'fraction_non_alphanumeric': 0.08128640162066346, 'fraction_numerical': 0.011142061281337047, 'mean_word_length': 3.055441478439425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 8, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6754215', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1155, 'n_words': 477}