Receiving Realtime Messages with Firebase2:56 with Michael Lustig
What good does sending a message do if we can’t receive them? In this video, I’ll show you how to listen to changes in your Firebase and update your messages list when a new user posts a message.
Similar to having a conversation, 0:00 in fire base we receive real time updates to our data by listening for them. 0:02 Fire base provides an interface called child even listeners, that notifies us 0:07 when children of a give node have moved, been added, changed, or deleted. 0:11 In this application, since we're keeping it as simple as possible, 0:16 we're only going to worry about when a message is added to our messages node. 0:19 Let's show you what that looks like in code. 0:24 Hopping over into Android Studio, I'll open the MessagingActivity. 0:25 You remember that child event listener I mentioned just a second ago? 0:29 Let's have our activity implement that interface, so 0:34 we can indicate to Firebase that this activity knows what to do 0:36 with ChildEvents, such as messages being added and removed from our messages node. 0:39 Although we need to implement each of these methods from the interface 0:46 we're only going to provide a method body for onChildAdded. 0:49 In a real application, 0:52 you'd more than likely wanna provide some kind of logic for each event. 0:53 But we wanna get up and running with Firebase as quickly as possible, so 0:57 we'll only provide proper functionality for that one method. 1:00 Let's think about what we wanna do here. 1:03 Whenever Firebase tells us there's a new message, we wanna 1:06 add that message to our recycler view so that it is displayed on the screen. 1:08 So let's do just that. 1:12 Another cool thing that Firebase gives us is the easy serialization and 1:14 deserialization of our data. 1:18 That means that since we posted our message object as a child of the messages 1:20 reference. 1:23 We can simply request a messages object back from the dataSnapshot by passing 1:24 the Message.class, class literal to the dataSnapshot.getValue method. 1:28 Now that we have a message object, we'll call the addMessage method on our recycler 1:34 view's message adapter, and scroll down to the most recent message. 1:38 Finally, we've written code for what we wanna happen when changes occur in 1:42 their database, but we haven't indicated to Firebase that we can handle them. 1:45 To do that, we'll tell our messages reference that this activity knows how to 1:50 listen to changes in its data by calling the add ChildEventListener and 1:54 passing this, for this activity. 1:57 Let's run that code and verify that this works. 2:03 I'll also pull up an additional emulator in the Firebase real time database from 2:06 the console to verify to you that indeed this is syncing in real 2:10 time across all clients. 2:13 That just blows my mind, I hope you find it as exciting as I do. 2:31 Through this workshop I hope you've gotten a great glimpse 2:36 into how powerful Firebase's Realtime Database functionality truly is. 2:39 The possibilities of what it can do are truly limitless, and 2:44 I hope that we've helped you gain confidence in incorporating Firebase into 2:47 your applications, or perhaps sparked new ideas that you'll embark on next. 2:50 Thanks for watching. 2:54
You need to sign up for Treehouse in order to download course files.Sign up