                 // Inform JSQMessagesViewController that a message has been received.
                 self.finishReceivingMessage()
                 
-                
             }
             
-            
-            
-        }
-        
+        }
+        
+    }
+    
+    // observer user typing object from Firebase
+    private func observeTyping() {
+        // This method creates a reference to the URL of /typingIndicator, which is where you’ll update the typing status of the user. You don’t want this data to linger around after users have logged out, so you can delete it once the user has left using onDisconnectRemoveValue().
+        
+        let typingIndicatorRef = rootRef.childByAppendingPath("typingIndicator")
+        userIsTypingRef = typingIndicatorRef.childByAppendingPath(senderId)
+        userIsTypingRef.onDisconnectRemoveValue()
+        
+        
+        // initialize the query by retrieving all users who are typing. This is basically saying, “Hey Firebase, go to the key /typingIndicators and get me all users for whom the value is true.”
+        usersTypingQuery = typingIndicatorRef.queryOrderedByValue().queryEqualToValue(true)
+        
+        // Observe for changes using .Value; this will give you an update anytime anything changes.
+        usersTypingQuery.observeEventType(FEventType.Value) { (data:FDataSnapshot!) -> Void in
+            
+            // You're the only typing, don't show the indicator
+            if data.childrenCount == 1 && self.isTyping {
+                return
+            }
+            
+            // Are there others typing?
+            self.showTypingIndicator = data.childrenCount > 0
+            self.scrollToBottomAnimated(true)
+        }
+        
+
+    }
+    
+    
+    
+    // Mark: textView delegate
+    override func textViewDidChange(textView: UITextView) {
+        super.textViewDidChange(textView)
+        
+        // If the text is not empty, the user is typing
+         isTyping = textView.text != ""
         
     }
     

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.observeMessages()
    }
    
    

        // Start by creating a query that limits the synchronization to the last 25 messages.
        let messagesQuery = messageRef.queryLimitedToLast(25)
        
        // Use the .ChildAdded event to observe for every child item that has been added, and will be added, at the messages location.
        messagesQuery.observeEventType(FEventType.ChildAdded) { (snapshot:FDataSnapshot!) -> Void in
            
            // Extract the senderId and text from snapshot.value.
            
            if let id = snapshot.value["senderId"] as? String, text = snapshot.value["text"] as? String {
                
