Firebase Tutorial: Real-time Chat

Download the finished project and compare your code with this code.
Was the solution for me.
Still one issue. Photoā€™s load on the simulator when added trough the chat. But in de iPhone nothing happensā€¦Perhaps i missed something in the plist file.

images first load but now they donā€™t and they donā€™t present in my iPhone.
But they do in the simulatorā€¦?

great tutorial as always!

one question: how can we reduce the image data?
thanks!

Really great tutorial! I was able to achieve similar functionality in no time thanks to your tutorial.

As side note, in the ā€œsend imagesā€ section, within the code handling photo from photo library

there is a code line:

let path = "\(FIRAuth.auth()?.currentUser?.uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(photoReferenceUrl.lastPathComponent)"

that generates a weird url back in the firebase storage due to unwrapped optional.

I quickly fixed it by replacing it with:

let path = "\(FIRAuth.auth()!.currentUser!.uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(photoReferenceUrl.lastPathComponent)"

force unwrapping it. Of course this is the faster solution but not the safest one.

Thanks

Thank for this. One question: Why do we not have a capture list in the logging in section? We are using self in there, should we not do an unowned?

Sir, i need same firebase chat tutorial for android. I have searched, your url redirecting to here only for iOS.

Firebase vs. Firebase Cloud Messagingā€¦

It seems from my Googling that this August saw the release of FCM, suggesting, if I understand correctly, a possible alternative to using Firebase Storage and Firebase Database for chat. Any thoughts on the pros and cons of one versus the other?

Yeah, FCM (Firebase Cloud Messaging for those following along at home) is more complex. It requires you setup your own app server that adheres to their protocols. So itā€™s not a fully hosted solution like Firebase Storage et al.

Ahh, thanks for illuminating this confusing subject. Thinking proper Firebase Storage, etc. is where itā€™s at then. Hoping it doesnā€™t disappear like Parse or soar up in price.

There is a retain cycle in the project. When you create the ChatViewController it will never be deleted from memory. You have the deinit method but the method is not getting called. I also checked it via Instruments Allocations. So far I donā€™t know how to fix it, but itā€™s a serious bug. Any ideas?

Hi @aetsyss, it looks like youā€™re right. Well spotted! :]

Iā€™ve just had a quick look and canā€™t see anything obviously retaining the ChatViewController. Iā€™ll try and take another look over the next few days, but Iā€™ve got another tutorial on the go and should be spending my time on that one at the moment!

In the meantime, if you find the problem please do let me know and Iā€™ll update the tutorial. Thanks!

Hi @tomelliott,
I managed to fix the retain cycle by placing [weak self] before all blocks where I access self in the ChatViewController.

Ah-ha! Iā€™ve just confirmed with a quick change and that fixes it for me. Thanks for debugging @aetsyss! :]

Iā€™m somewhat surprised those retain cycles keep the view controller around indefinitely. As theyā€™re basically just async API calls Iā€™d have thought everything would die eventually when the calls either complete or time out (as the blocks will die), but clearly you canā€™t rely on that. Iā€™ll take a closer look over the next few days - maybe one in particular is playing up.

Glad to help you! :slight_smile:

I keep getting an error: no such module found ā€˜JSQMessagesViewControllerā€™

pod use_frameworks!
pod ā€˜JSQMessagesViewControllerā€™

Iā€™ve update the pod, tried to do a fresh import of JSQMessageViewController.

Anyone have any expert advice?

1 Like

me too the same issue:
no such module: JSQMessagesViewController

https://drive.google.com/file/d/0B86KWEseOAWKYXBUOFdidWVzcDA/view?usp=sharing
I am having a crashing problem!! PLEASE HELP!!

https://drive.google.com/file/d/0B86KWEseOAWKYXBUOFdidWVzcDA/view?usp=sharing
I am having a crashing problem!! PLEASE HELP!!

Hi, looks like uā€™ve got corrupted data. Try this to handle:

 private func observeChannels() {
    // We can use the observe method to listen for new
    // channels being written to the Firebase DB
    channelRefHandle = channelRef.observe(.childAdded, with: { (snapshot) -> Void in
        if let channelData = snapshot.value as? Dictionary<String, AnyObject> {
            let id = snapshot.key
            if let name = channelData["name"] as! String!, name.characters.count > 0 {
                self.channels.append(Channel(id: id, name: name))
                self.tableView.reloadData()
            } else {
                print("Error! Could not decode channel data")
            }
        }
    }
  )}

Great tutorial TIm! Couple of questions:

  • With anonymous access authentication enabled on Firebase database, how do you define database rules to prevent public access to the database?

  • How do you add a badge ā€œblue dot like emailā€ to channel for unread messages?

Thanks!