           clearUserHeartbeat(user,clientId)
           broadcastStatus(user,"offline")
 
-      clearInterval(hearbeatInterval)
+      clearInterval(hearbeatInterval) if hearbeatInterval
 
 setUserHeartbeat = (user,time) ->
   userId   = user._id

  callback(message)

startHeartbeat = (userId,clientId) ->
  setInterval ->
    refreshPresence(userId,clientId,this)
  , 30000

refreshPresence = (userId,clientId,hearbeatInterval) ->
  mongodb.collection 'users', (err, collection) ->
    console.log err if err
    collection.findOne { _id: new mongo.ObjectID(userId) }, (err,user) ->
      console.log err if err
      checkStatusAndBroadcast(user,clientId,hearbeatInterval) if user != null

