         
     }
     
+    //Function to add the incoming messages
+    func didReceiveUpdate(notification: NSNotification) {
+        
+        print("Notification: \(notification.userInfo!)")
+        var type = ""
+        if let t = notification.userInfo!["t"]{
+            type = t as! String
+        }
+        self.users[self.users.count] = [notification.userInfo!["u"]!["_id"]! as! String, notification.userInfo!["u"]!["username"]! as! String, notification.userInfo!["msg"]! as! String,type]
+        
+        let timestamp = [notification.userInfo!["ts"]!["$date"] as! NSNumber]
+        let timestampInDouble = timestamp as! [Double]
+        self.ts[self.ts.count] =  [timestampInDouble[0] / 1000]
+        
+        //Reloading data and if we are at the bottom scroll the view to the last row
+        self.mainTableview.reloadData()
+        
+        let lastVisibleCells = self.mainTableview.visibleCells.last
+        let lastVisibleCellsIndexPath = NSIndexPath(forRow: self.mainTableview.indexPathForCell(lastVisibleCells!)!.row, inSection: 0)
+        
+        self.bottomIndexPath = NSIndexPath(forRow: self.users.count - 1, inSection: 0)
+        
+        if (lastVisibleCellsIndexPath.row == (self.users.count - 2)) {
+            
+            //            self.mainTableview.scrollToRowAtIndexPath(NSIndexPath(forRow: self.users.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
+            self.mainTableview.scrollToRowAtIndexPath(self.bottomIndexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
+            
+            
+        }
+        
+        
+    }
     
 }
    //Number of table rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
//        return messages.count + 1 //plus 1 to put the loadMore button at the top
      return 1
      
    }
    
    //Populating data
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        

//        //Get visible cells indexes
//        let visible = mainTableview.indexPathsForVisibleRows
//        
//        //Set bottomIndexpath to last visible cell's index
//        bottomIndexPath = NSIndexPath(forRow: visible!.last!.row, inSection: 0)
      
      let cell = UITableViewCell()
        
//        //Boolean to check if previous and current user are the same user
//        var sameUser = false
//        
//        
//        //Get current row
//        let row = indexPath.row - 1 //minus 1 to stay in range of the array
//        
//        //Get previous row
//        let prevRow = row - 1
//        
//        
//        //Check previous and current user
//        if(row > 0){
//            
//
//          // TODO: need to check if 2 messages have the same owner.
//          //            if(cR1!.messages[row].user == cR1!.messages[prevRow].user){
//          
//                //If true then set sameUser to true
//                sameUser = true
//            
////            }
//        }
//        
//        
//        
//        //Create cell and set data
//        
//        //if we scrolled at the top
//        if (indexPath.row == 0) {
//            
//            //try create the cell for the load button
//            var loadMoreHeader:LoadMoreHeaderTableViewCell? = mainTableview.dequeueReusableCellWithIdentifier("loadMoreHeader", forIndexPath: indexPath) as? LoadMoreHeaderTableViewCell
//            
//            //if the cell is nil
//            if loadMoreHeader == nil{
//                
//                //create it
//                loadMoreHeader = LoadMoreHeaderTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "loadMoreHeader")
//                
//            }
//            
//            //return it
//            return loadMoreHeader!
//        }
//        
//        
//        //If same user
//        if sameUser {
//            
//            //Try to create cell
//            var noDetailsCell:NoDetailsTableViewCell? = mainTableview.dequeueReusableCellWithIdentifier("noDetailsCell", forIndexPath: indexPath) as? NoDetailsTableViewCell
//            
//            //If it is nill
//            if noDetailsCell == nil{
//                
//                //Create the cell
//                noDetailsCell = NoDetailsTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "noDetailsCell")
//            
//            }
//            
//            //Set hidden timestamp
//            let defaultTimeZoneStr = dateFormatter.stringFromDate(cR1!.messages[indexPath.row - 1].tstamp)
//            print(defaultTimeZoneStr)
//            noDetailsCell!.hiddenTimeStamp.text = "\(defaultTimeZoneStr)"
//            noDetailsCell!.hiddenTimeStamp.hidden = true
//            noDetailsCell!.hiddenTimeStamp.textColor = UIColor.rocketTimestampColor()
//
//            //Set text to noDetailsMessage label
//            noDetailsCell!.noDetailsMessage.text = "\(cR1!.messages[indexPath.row - 1].text)"
//            
//            //Set color to #444444
////            noDetailsCell!.noDetailsMessage.textColor = UIColor.colorWithHexValue(44, greenValue: 44, blueValue: 44, alpha: 1)
//            noDetailsCell!.noDetailsMessage.textColor = UIColor.rocketSecondaryFontColor()
//            
//            //return the no detailed cell
//            return noDetailsCell!
//            
//        }
//        //If different user
//        else{
//            
//            //Try to create the cell
//            var fullDetailsCell:MainTableViewCell? = mainTableview.dequeueReusableCellWithIdentifier("fullDetailsCell", forIndexPath: indexPath) as? MainTableViewCell
//            
//            //If nill
//            if fullDetailsCell == nil{
//                
//                //Create the cell
//                fullDetailsCell = MainTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "fullDetailsCell")
//                
//                
//            }
//            
//            //Set the image
//            fullDetailsCell!.avatarImg.image = UIImage(data: (cR1?.messages[indexPath.row - 1].user.avata)!)
//            
//            //Set the text for the username label
//            fullDetailsCell!.usernameLabel.text = "\(cR1!.messages[indexPath.row - 1].user.username)"
//            
//            //Set color to #444444
////            fullDetailsCell!.usernameLabel.textColor = UIColor.colorWithHexValue(44, greenValue: 44, blueValue: 44, alpha: 1)
//            fullDetailsCell!.usernameLabel.textColor = UIColor.rocketMainFontColor()
//            
//            //Set the timestamp
//            let defaultTimeZoneStr = dateFormatter.stringFromDate(cR1!.messages[indexPath.row - 1].tstamp)
//            print(defaultTimeZoneStr)
//            fullDetailsCell!.timeLabel.text = "\(defaultTimeZoneStr)"
//            fullDetailsCell!.timeLabel.textColor = UIColor.rocketTimestampColor()
//            
//            //Set the message text
//            fullDetailsCell!.messageLabel.text = "\(cR1!.messages[indexPath.row - 1].text)"
//            
//            fullDetailsCell!.messageLabel.textColor = UIColor.rocketSecondaryFontColor()
//            
//            //Return the full detailed cell
//            return fullDetailsCell!
//        }
      return cell
        
    }
    

                return
            }else{
                self.composeMsg.text = ""
//                print(response)
            }
            
        })
