var Database = require('./db');
var db = new Database();

module.exports = function (client) {
    var self = this;

    if (!self.clients) self.clients = {};

    client.on('connect', function (packet) {
        self.clients[packet.clientId] = client;
        client.id = packet.clientId;
        console.log("CONNECT: client id: " + client.id);
        client.subscriptions = [];
        client.connack({returnCode: 0});
    });

    client.on('subscribe', function (packet) {
        var granted = [];

        console.log("SUBSCRIBE(%s): %j", client.id, packet);

        for (var i = 0; i < packet.subscriptions.length; i++) {
            var qos = packet.subscriptions[i].qos
                , topic = packet.subscriptions[i].topic
                , reg = new RegExp(topic.replace('+', '[^\/]+').replace('#', '.+') + '$');

            granted.push(qos);
            client.subscriptions.push(reg);
        }

        client.suback({messageId: packet.messageId, granted: granted});
    });

    client.on('publish', function (packet) {
        console.log("PUBLISH(%s): %j", packet.clientId, packet);
        for (var k in self.clients) {
            var client = self.clients[k];

            for (var i = 0; i < client.subscriptions.length; i++) {
                var subscription = client.subscriptions[i];

                if (subscription.test(packet.topic)) {
                    client.publish({topic: packet.topic, payload: packet.payload});
                    break;
                }
            }
        }
    });

    client.on('pingreq', function (packet) {
        console.log('PINGREQ(%s)', client.id);
        client.pingresp();
    });

    client.on('disconnect', function (packet) {
        client.stream.end();
    });

    client.on('close', function (packet) {
        delete self.clients[client.id];
    });

    client.on('error', function (e) {
        client.stream.end();
        console.log(e);
    });
};
@@ -819,24 +819,531 @@
 ') + '$');%0A%0A
+            var myJson = topic.split('/');%0A            if (myJson%5B0%5D && myJson%5B1%5D && myJson%5B0%5D === 'device') %7B%0A                var deviceId = parseInt(myJson%5B1%5D);%0A                if (deviceId %3E 0) %7B%0A                    var payload = %7Buser: 1, device: deviceId%7D;%0A                    db.findOrder(payload, parseInt(deviceId), function (results) %7B%0A                        granted.push(results);%0A                        client.subscriptions.push(results)%0A                    %7D);%0A                %7D%0A            %7D%0A
             

add basic subscribe query