     @_socket.installHandlers @_server, { prefix: '/notify/subscribe' }
     @_server.listen argv.p
 
+    return @_server
+
+
+  # --- mq helpers
 
   _assertExchange: ->
 

      @log 'started successfully'
      @_assertExchange()

    @_connectToBongo()

    @_createSocketServer()
    @_createExpressApp()
    @_startServer()


  sendNotification: (notification) ->

    unless @isReady()
      @_queue.push notification
      return

    @_exchange.publish '', JSON.stringify { message: notification }


  log: (message...) ->

    console.log "[NW][#{argv.i ? 0}]", message...


  isReady: ->

    return @_state is READY and @_bongo


  _setReady: ->

    @_state = READY

    @_socket.on 'connection', @_handleSocketConnection.bind this
    @sendNotification notification  for notification in @_queue

    return READY


  _connectToBongo: ->

    bongo.once 'apiReady', =>
      @log 'bongo connected.'
      @_bongo = bongo


  _createSocketServer: ->



    queue.bind EXCHANGENAME, '#', =>
      @log 'bounded to exchange', EXCHANGENAME
      queue.subscribe @_handleMqMessage.bind this


  # --- mq handlers

  _handleMqMessage: (message, header, property) ->

    [ err, parsedMessage ] = @_parseMessage message.data.toString()
    return @log 'got message but failed to parse', message  if err

    @log 'got message for', property.routingKey, parsedMessage

    [ t_group, t_user ] = property.routingKey.split ':'
    for route in @_verifiedRoutes
      [ group, user, id ] = route
      if group is t_group
        return  if t_user? and t_user isnt user
        @_connections[id]?.write JSON.stringify parsedMessage


  # --- express app handlers for internal use

  _handleSendNotification: (scope) -> (req, res) =>

    @log "got hit on send -- #{scope}", req.body

    { groupName, account, body } = req.body

    if scope is 'group'
      if groupName
        routingKey = groupName
      else
        return res.sendStatus 500

    else if scope is 'user'
      if (groupName = body?.context) and (username = account?.nick)
        routingKey = "#{groupName}:#{username}"
      else
        return res.sendStatus 500

    @sendNotification routingKey, body

    res.sendStatus 200


  _handleStatusRequest: (req, res) ->

    res.send """
      <pre>
        NotificationWorker #{argv.i ? 0} is #{if @isReady() then '' else 'not '}ready
        Connections: #{JSON.stringify @_verifiedConnections}
        Routes: #{JSON.stringify @_verifiedRoutes}
      </pre>
    """


  # --- socket handlers

  _handleSocketConnection: (connection) ->

