 
       dispatcherRegistered = false
       dispatcher = =>
-        entered = $pluck entered, 'val'
-        enterEvent = if entered.length
-          JSON.stringify {
-            jsonrpc: '2.0'
-            method: 'all'
-            params: {
-              type: 'enter'
-              items: entered
-            }
-          }
-        exited = $pluck exited, 'val'
-        exitEvent = if exited.length
-          JSON.stringify {
-            jsonrpc: '2.0'
-            method: 'all'
-            params: {
-              type: 'exit'
-              items: exited
-            }
-          }
-
-        if entered.length
-          connection.send enterEvent for id, connection of @connections
-        if exited.length
-          connection.send exitEvent for id, connection of @connections
+        unless $isEmpty entered
+          enterParams =
+            type: 'enter'
+            items: $pluck entered, 'val'
+          for id, connection of @connections
+            connection.notify 'all', enterParams
+
+        unless $isEmpty exited
+          exitParams =
+            type: 'exit'
+            items: $pluck exited, 'val'
+          for id, connection of @connections
+            connection.notify 'all', exitParams
         dispatcherRegistered = false
         entered = {}
         exited = {}
 
+      # TODO: maybe close events (500ms) could be merged to limit
+      # network consumption.
       @_xobjs.on 'any', (event, items) ->
         unless dispatcherRegistered
           dispatcherRegistered = true

# Cryptographic tools.
$crypto = require 'crypto'

# Events handling.
{EventEmitter: $EventEmitter} = require 'events'

#---------------------------------------------------------------------

# Redis.
$createRedisClient = (require 'then-redis').createClient

$forEach = require 'lodash.foreach'

# Password hashing.
$hashy = require 'hashy'

$isString = require 'lodash.isstring'

$pluck = require 'lodash.pluck'

$Promise = require 'bluebird'

#---------------------------------------------------------------------

# A mapped collection is generated from another collection through a
# specification.
{$MappedCollection} = require './MappedCollection'

# Collection where models are stored in a Redis DB.
$RedisCollection = require './collection/redis'

# Base class for a model.
$Model = require './model'

# Connection to XAPI.
$XAPI = require './xapi'

# Helpers for dealing with fibers.
{$fiberize, $wait} = require './fibers-utils'

$Connection = require './connection'

#=====================================================================

# Promise versions of asynchronous functions.
$randomBytes = $Promise.promisify $crypto.randomBytes

$hash = $hashy.hash
$needsRehash = $hashy.needsRehash
$verifyHash = $hashy.verify

#=====================================================================
# Models and collections.

      retrievableTypes = do ->
        methods = $wait xapi.call 'system.listMethods'

        $debug 'connected to %s@%s', server.username, server.host

        types = []
        for method in methods
          [type, method] = method.split '.'
