const emitter = require('emitter-io');

module.exports = function (RED) {
    let status;
    let client;

    function CenConfigNode(config) {
        RED.nodes.createNode(this, config);

        let self = this;

        self.users = {};    

        self.host = config.host;
        self.user = config.user;
        self.channel = config.channel || 'public/';
        self.secret = self.credentials.secret;
        self.info = "";

        let client = emitter.connect({
            host: self.host,
            port: 8800,
            keepalive: 30,
            secure: false
        });
        client.on('message', function(message){
            var msg = message;
            console.log(message);


            Object.keys(self.users).forEach(function (id) {
                self.users[id].emit("input", {
                    // {
                    //   "uid":"kJCJOjJBnm4BrGsyN1pmcm",
                    //   "client":"9ec561f3-abbb-452e-af76-2831704cdc41",
                    //   "info":{"user":"auto","client":"9ec561f3-abbb-452e-af76-2831704cdc41"},
                    //   "channel":"public",
                    //   "data":{"input":"Www"},
                    //   "_msgid":"fd1c347c.48f708"}
                    //
                });
            });        
        });
        client.subscribe({
            key : self.secret,
            channel : self.channel
        });

        this.register = function (clientNode) {
            self.users[clientNode.id] = clientNode;
        };

        this.deregister = function (clientNode, done) {
            delete self.users[clientNode.id];
            return done();
        };

        this.setStatus = function (c) {
            status = c;
            let s;
            switch (c) {
                case 'connecting':
                    s = {
                        fill: 'yellow',
                        shape: 'ring',
                        text: 'node-red:common.status.connecting'
                    };
                    break;
                case 'prompt':
                    s = {
                        fill: 'yellow',
                        shape: 'ring',
                        text: c
                    };
                    break;
                case 'connect':
                    s = {
                        fill: 'green',
                        shape: 'dot',
                        text: 'node-red:common.status.connected'
                    };
                    break;
                case 'disconnected':
                    s = {
                        fill: 'red',
                        shape: 'ring',
                        text: 'node-red:common.status.disconnected'
                    };
                    break;
                default:
                    s = {
                        fill: 'red',
                        shape: 'ring',
                        text: c
                    };
            }

            Object.keys(self.users).forEach(function (id) {
                self.users[id].status(s);
            });
        };
    }

    // !Register
    RED.nodes.registerType('em-config', CenConfigNode, {
        credentials: {
            secret: {
                type: 'text'
            }
        }
    });
};

@@ -428,16 +428,54 @@
  = %22%22;%0A%0A
+        self.setStatus('connecting');%0A
         
@@ -733,20 +733,101 @@
 (message
-);%0A%0A
+.asObject());%0A%0A            // broadcast to each %60em-input%60 nodes that use this config
 %0A       
@@ -950,12 +950,19 @@
     
-// %7B
+%22uid%22 : %22%22,
 %0A   
@@ -982,118 +982,53 @@
     
-//   %22uid%22:%22kJCJOjJBnm4BrGsyN1pmcm%22,%0A                    //   %22client%22:%229ec561f3-abbb-452e-af76-2831704cdc41%22,
+%22client%22 : %22%22,%0A                    %22info%22 : %7B
 %0A   
@@ -1048,128 +1048,134 @@
     
-//
    
-%22info%22:%7B%22user%22:%22auto%22,%22client%22:%229ec561f3-abbb-452e-af76-2831704cdc41%22%7D,%0A                    //   %22channel%22:%22public%22
+ %22user%22 : %22%22,%0A                        %22client%22 : %22%22,%0A                    %7D,%0A                    %22channel%22 : message.channel
 ,%0A  
@@ -1185,29 +1185,24 @@
             
-   //
    %22data%22:%7B%22
@@ -1202,24 +1202,29 @@
 ata%22
-:%7B%22input%22:%22Www%22%7D
+ : message.asObject()
 ,%0A  
@@ -1238,21 +1238,16 @@
         
-   //
    %22_msg
@@ -1253,27 +1253,33 @@
 gid%22
-:%22fd1c347c.48f708%22%7D
+ : %22%22%0A                %7D);
 %0A   
@@ -1287,24 +1287,27 @@
         
+%7D);
         
 //%0A     
@@ -1302,35 +1302,208 @@
     
-//%0A                %7D);%0A    
+%0A        %7D);%0A        client.on('connect', function (con) %7B%0A            self.setStatus('connect');%0A        %7D);%0A        client.on('disconnect', function () %7B%0A            self.setStatus('disconnected');%0A
     
@@ -1505,32 +1505,33 @@
 %0A        %7D);
+%0A
         
 %0A        %7D);
@@ -1510,32 +1510,105 @@
     %7D);%0A        
+client.on('error', function (err) %7B%0A            self.setStatus('prompt');
 %0A        %7D);%0A   
@@ -1596,32 +1596,33 @@
 ');%0A        %7D);%0A
+%0A
         client.s

Add connection state to emitter node