     {@clientId} = channel
     channel
 
+  verifyEvent: (event) ->
+    {site, id} = event
+    lastId = @lastEventIdsBySite[site]
+    if lastId? and id isnt lastId + 1
+      console.error("Expected next event to be #{lastId + 1} but got #{id} for site #{site}")
+    @lastEventIdsBySite[site] = id
+
+  stampEvent: (event) ->
+    event.id = @nextOutgoingEventId++
+    event.site = window.site.id
+
   connectDocument:  ->
     @doc.on 'replicate-change', (event) =>
+      @stampEvent(event)
       @channel.send('document-changed', event)
 
     @channel.on 'document-changed', (event) =>
+      @verifyEvent(event)
       @doc.applyRemoteChange(event)
 
   snapshotRepository: (callback) ->

  _.extend @prototype, require('event-emitter')

  constructor: ({@site, @id, @host, @port, @secure}) ->
    @secure ?= config.get('collaboration.secure') ? true
    @host ?= config.get('collaboration.host') ? 'fallout.in'
    @participants = []

  start: ->
    @channel = @subscribe(@id)

    @channel.on 'channel:closed', =>
      @listening = false
      @trigger 'stopped'

