         # '#' is 'match anything to the end of the string' */
         # + is 'match anything but a / until you hit a /' */
         reg = new RegExp(subscription.topic.replace('+', '[^\/]+').replace('#', '.+$'));
-        client.subscriptions.push(reg);
+        client.subscriptions.push(reg)
 
     client.on 'publish', (packet) ->
-      
-      try 
-        data[packet.topic] = { json: true, payload: JSON.parse(packet.payload) }
-      catch e
-        data[packet.topic] = { json: false, payload: packet.payload }
-
-	    # Iterate over our list of clients
-	    for client in mqtt_client_list
-	      # Iterate over the client's subscriptions
-        for subscription in client.subscriptions
-          # If the client has a subscription matching
-          # the packet...
-
-          c.publish(packet.topic, packet.payload) if subscription.test(packet.topic)
-
+      publish_payload packet.topic, packet.payload
 
     client.on 'pingreq', (packet) ->
 	    client.pingresp()

module.exports = (app) ->
  app.get '/topics/:topic', (req, res) ->
    topic = req.params.topic
    if data[topic]?
      if data[topic].json
        res.contentType('json')
        res.send data[topic].payload
      else 
        # it's not a json, don't emit anything
        res.send data[topic].payload
    else
      res.send "Topic not found!", 404

module.exports.start = (port) ->
  mqtt.server.listen(port)


mqtt_client_list = []

io = null # this will be defined in the start method

data = {}

publish_payload = (topic, payload) ->
  # emit the payload over mqtt
  # Iterate over our list of mqtt clients
  for client in mqtt_client_list
    # Iterate over the client's subscriptions
    for subscription in client.subscriptions
      # If the client has a subscription matching
      # the packet...

      c.publish(topic, payload) if subscription.test(topic)

  # store the payload for REST consumption
  try
    data[topic] = { json: true, payload: JSON.parse(payload) }
  catch e
    data[topic] = { json: false, payload: payload }

  # emit the payload over websocket
  io.sockets.emit "/topics/#{topic}", data[topic].payload

mqtt.on 'new_client', (client) ->
    console.log("New client emitted")
