 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
+    if req.accepts 'json'
+      if data[topic]?
+        if data[topic].json
+          res.contentType('json')
+          res.send data[topic].payload
+        else
+          # it's not a json, don't emit a contentType
+          res.send data[topic].payload
+      else
+        res.send "Topic not found!", 404
     else
-      res.send "Topic not found!", 404
+      value = data[topic] || { json: false, payload: "" }
+      res.render 'topic.hbs', topic: req.params.topic, value: value
+
+  # setup websockets
+  io = require('socket.io').listen(app)
 
 module.exports.start = (port) ->
   mqtt.server.listen(port)


mqtt_client_list = []

data = {}

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

        # '#' 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.on 'publish', (packet) ->
      publish_payload packet.topic, packet.payload

    client.on 'pingreq', (packet) ->
	    client.pingresp()
