 WebSocket = require 'ws'
+WebSocketServer = require('ws').Server
+
 {EventEmitter} = require 'events'
 {Scrubber, Store} = require 'koding-dnode-protocol'
 noop = ->
 
+
 class KiteClient extends EventEmitter
-    constructor: (@remoteAddr='ws://localhost:4000/dnode')->
+    constructor: (@options, @remoteAddr='ws://localhost:4000/dnode')->
       @localStore = new Store
       @remoteStore = new Store
+
+      self = this
+      @api =
+        heartbeat: (k)->
+          console.log ">>>>>", "heartbeat called", k.withArgs[1]
+          remoteFn = k.withArgs[1]
+          setInterval ()->
+            remoteFn()
+          , 1000
 
     connect: (callback=noop)->
       @ws = new WebSocket @remoteAddr


      @ws.on 'message', (data, flags)->
        # now we descrub message and call our callback function
        scrubbed = JSON.parse data
        scrubber = new Scrubber @localStore
        unscrubbed = scrubber.unscrub scrubbed
        callback = self.localStore.get(scrubbed.method)
        if callback
            callback.apply this, unscrubbed

    callRemote: (method, args, authentication, callback=noop)=>
      scrubber = new Scrubber @localStore
      # adding required stuff
      payload = {}
      payload.kite =
        name: "application"
        username: "devrim"
        id: "a8877ba5-9b18-420b-792a-e561316b3257"
        environment: 'development'

        version: "1" # <--- this should always be a string, dont ask me why, its not an integer
        hostname: "aybarss-MacBook-Air.local"
        publicIP:""
        port: @options.port.toString() or null # <-- port should be string.
      payload.withArgs = args
      payload.links = []

