   true: => true
 
   popMessage: (callback) =>
-    redis.brpop 'meshblu-messages', 1, (err, result) =>
+    redis.client.brpop 'meshblu-messages', 1, (err, result) =>
       return callback err if err?
       return callback() unless result?
       [queueName, jobStr] = result

async = require 'async'
redis = require './lib/redis'
authDevice = require './lib/authDevice'
sendMessageCreator = require './lib/sendMessage'
createMessageIOEmitter = require './lib/createMessageIOEmitter'

class Worker
  constructor: ->
    @_sendMessage = sendMessageCreator createMessageIOEmitter()

  run: =>
    async.whilst @true, @popMessage, (error) =>

      @processJob job, callback

  processJob: (job, callback) =>
    {auth,message} = job
    {uuid,token} = auth

    authDevice uuid, token, (error, device) =>
      return callback error if error?

      @sendMessage device, message, callback

  sendMessage: (device, message, callback) =>
    @_sendMessage device, message
    callback()

worker = new Worker()
