     @defaultHeaders = _.extend @defaultHeaders, opts.headers if opts?.headers
     @defaultHeaders.Authorization = "Basic " + (new Buffer("#{opts.username}:#{opts.password}").toString('base64')) if opts.password and opts.username
 
+    @on 'batch', @__request
+    tasks = []
+    @on 'query', (id, task) => 
+      tasks.push task
+      if @ready
+        @emit 'batch', tasks, -> tasks = [] 
+    
     @__connect()
-    @__start()
-
-  __start: ->
-    Meteor.setInterval =>
-      if @ready and Object.keys(@batchQueue).length > 0
-        tasks = []
-        clones = {}
-        for taskId, task of @batchQueue
-          tasks.push _.clone task
-          clones[taskId] = _.clone @batchQueue[taskId]
-
-          @batchQueue[taskId] = undefined
-          delete @batchQueue[taskId]
-        
-        @__call @__service.batch.endpoint
-        , 
-          data: tasks
-          headers:
-            Accept: 'application/json; charset=UTF-8'
-            'Content-Type': 'application/json'
-        ,
-          'POST'
-        ,
-          (error, results) =>
-            if results?.data
-              for result in results.data
-                if clones?[result.id]
-                  if result?.body
-                    if _.isEmpty result.body?.errors
-                      @batchResults[result.id] = result.body
-                    else
-                      for error in result.body.errors
-                        console.error error.message
-                        console.error {code: error.code}
-                      @batchResults[result.id] = []
-                  else
-                    @batchResults[result.id] = []
-
-                  clones[result.id] = undefined
-                  delete clones[result.id]
-
-    , 100
-
-  __getBatchResult: (id, cb) ->
-    i = 0
-    timerId = Meteor.setInterval () =>
-      if @batchResults?[id]
-        if 1 <= id <= 999999
-          reactive = true
-        else
-          reactive = false
-
-        result = @__transformData _.clone(@batchResults[id]), reactive
-        
-        listener.call null, null, _.clone result for id, listener of @listeners
-
-        @batchResults[id] = undefined
-        delete @batchResults[id]
-        Meteor.clearInterval timerId
-        cb and cb null, result
-      else if i > 300
-        Meteor.clearInterval timerId
-        cb and cb new Error "Batch request timeout"
-      i++
-    , 100
-    return
+
+  __request: _.throttle (tasks, cb) ->
+    bound =>
+      @__call @__service.batch.endpoint
+      , 
+        data: tasks
+        headers:
+          Accept: 'application/json; charset=UTF-8'
+          'Content-Type': 'application/json'
+      ,
+        'POST'
+      ,
+        (error, results) =>
+          # console.log results
+
+          if results?.data
+            @__proceedResult result for result in results.data
+
+          else if results?.content
+            content = JSON.parse results.content
+            @__proceedResult result for result in content
+      cb and cb()
+
+  , 100
+
+  __proceedResult: (result) ->
+    if result?.body
+      if _.isEmpty result.body?.errors
+        @emit result.id, null, result.body
+      else
+        for error in result.body.errors
+          console.error error.message
+          console.error {code: error.code}
+        @emit result.id, null, []
+    else
+      @emit result.id, null, []
 
   __batch: (task, callback, reactive) ->
-    if reactive
-      task.id = Math.floor(Math.random()*(999999-1+1)+1)
-    else
-      task.id = Math.floor(Math.random()*(999999999-1000000+1)+1000000)
-
     task.to = task.to.replace @root, ''
-    @batchQueue[task.id] = task
+    @emit 'query', task.id, task
     unless callback
-      return Meteor.wrapAsync((cb) => @__getBatchResult task.id, cb)()
-    else
-      @__getBatchResult task.id, callback
+      res =  Meteor.wrapAsync((cb) =>
+        @once task.id, (error, response) =>
+          bound => cb error, @__transformData _.clone(response), reactive
+      )()
+      return res
+    else
+      @once task.id, (error, response) =>
+        bound =>
+          callback error, @__transformData _.clone(response), reactive
 
   __connect: -> 
     response = @__call @root

NTRU_def = process.env.NODE_TLS_REJECT_UNAUTHORIZED

class Neo4jListener
  constructor: (listener, @_db) ->

    delete @_db.listeners[@__id]

class Neo4jDB
  __proto__: events.EventEmitter.prototype;
  constructor: (@url, opts = {}) ->
    events.EventEmitter.call @
    @url = @url or process.env.NEO4J_URL or process.env.GRAPHENEDB_URL or 'http://localhost:7474'
    check @url, String
    check opts, Object
