Building an Android app with Google's Firebase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm Frank I work at what does it say I work at Google where I work on firebase I'm an engineer there and yeah we are right out here you might have seen us if you've got a headband you have some of our swag we also brought shirts hot sauce stickers we bought brought David East our new internet celebrity so he's going to be awesome and we're here to tell you a bit about firebase so I have 40 minutes for this talk which is not a lot so we'd better get started quickly and we're going to be talking about how to build apps with firebase so in this talk normally these talks writes lots of slides lots of people boring you with bullet points I don't really like bullet points so I have a few slides and then we're going to go to coding so I'm going to code an Android app from start to finish fingers crossed and if you liked what you see then at 2:30 we have a code lab it's right across the right across the hallway here and then you are going to be a live coding an Android app so that's going to be awesome so do join that one a bit about firebase before I start to coding let's set some context here so firebase is a it is made for a multi-device world so nowadays we all have multiple devices and whenever we have the same app on all of those devices we expect that when that day the app works together on all these devices so if you make a change to to my data on one app I see it on the others think of this as like the Google Docs nowadays right and this is great for me as a user I love it when in Google Docs when I have the app open on my desktop and on my phone if I make a change I immediately see it on the other screen also I love it as a developer or as a user as a developer I know that there's complex infrastructure behind this and so in case you don't realize it this is a Google Data Center most people think you can see that from the color of the wires actually you can mostly see it from the fact that it says Google Data Center at the bottom but this is what is behind something like Google Docs I just complex infrastructure behind these applications and this is fun for us as developers but our users don't care about it the users care about the features that you put in the app right in Mother in lingo despair if they can swipe left or right don't care about your beautiful databases so with firebase we allow you to focus on building your app so we are a platform that sits behind your mobile and web applications we a few statistics we have about four and a thousand developers registered we have about two million devices at any moments connected to our servers that last number I'm not sure if it seems seems very low I'm quite sure is higher the other one I'm pretty sure of and just in case we're not let's have a look at it right now so firebase is part of Google we're part of the Google cloud products and at the bottom of this page you can see the number of registered developers this is a live counter so if you sign up this counter will go up who here has already signed up for firebase yeah lots of hands or another so I sort of expect that you're gonna sign up now and that the counter goes up anyone okay it's okay if we don't but it is a live counter the rest of my talk is explaining how this counter works by the way so we have three main features we do a real-time database we do authentication and we do static hosting I'm going to be focusing most on the real-time database for now so who has done in the past a relational database good good good recently we were at a talk where and not a lot of hands went up that was scary so who has done anything with no sequel databases okay good I might need your help explaining things to the people that raised their hands initially so just so you know no sequel databases are very different in the case of firebase we are a cloud hosted hierarchical database that's a lot of words we are adjacent tree in the clouds so who has worked with Jason he's a great teammate and so we are a cloud hosted JSON database remember this slide we all have multiple devices if we install the app on all of these devices we expect that the day time between all of these devices is synchronized firebase makes this possible so we are a online hosted database and we automatically synchronize the data between the device that means that when any of these devices makes a change to the data that changes instantly synchronized to all other connected devices so within milliseconds the data shows up on all devices now by itself this is not magical what makes it magical is the way firebase does that so let's have a look like I said I mostly do code for a living so we're going to be live coding an Android app so it's sort of the world's ugliest or world's smallest Android chat app and so it's not going to be pretty as a trade-off for that I'm going to be coding it from start to finish I don't copy snippets I don't do weird expander type things everything is going to be live coded I do need something from you in exchange if I make any weird typos shout them out to me very loudly because otherwise we're going to be here for a long time okay firebase databases are cloud hosted which means that I can access them at a URL so at the bottom here you see a link I'm actually going to open it in a different tab I already did that that's not fair so this is a firebase database this is the dashboard for firebase that we see here let me zoom in a bit so this is a JSON tree and we visualize it for you you see in here that I have three top-level notes chats dpe bird mesh whatever that is and messages I have three different applications in a single database and we're going to be working with the chat application today so I click on it and i zoom in if you pay attention the URL in the address bar change because of that so I had nano chat with firebase iOS com I now went to slash chat under that every element in my Jason tree is uniquely addressable on the internet that means that if I go to this single chat message here URL changed again I only see that one if I only want to see this string this string has its own unique address so every value in your firebase database is uniquely addressable it's one of the unique points we do firebase has many SDKs so we have an Android SDK that I'm going to be using in a minute we also have a JavaScript SDK an iOS SDK and if those don't work for you we have a REST API the good thing about the REST API is that I can show it in a browser so if I type so jason at the end of the URL I get to jason spring detected location so this is the entire json data at this location this is a for the dos with rest this is a get request to this to this endpoint and I can also do put post delete so I can manipulate the data that way let's go back to the regular view so this is database we're going to be working with let's actually move it a bit to the side and start looking at the actual code that we're building so I have an emulator open and I have oh and that one rebooted earlier today my entire machine rebooted that was fun I hope that the Android emulator comes back up while we're waiting for that let's look at the end roid studio so who uses Android studio for the Android work good good good if your hand isn't up what are you using let's create a new project so I call this nano chet because it's like the minimal chat application I can build next up we need to select the SDK that we want to want to target so firebase works from API level I never remember what it is but something jellybeans I think that means that we work on ninety-nine point seven percent of the devices in this case I'm targeting API level 16 because we have a library that requires this we're still targeting 95% of all Android devices that are on the Play Store I'm going to create an empty activity let's call it main activity so while this is generating you see that my emulator came back up so that's good one of the disadvantages of life coding this app by the ways that we're going to be waiting for Gradle for a bit I will try to fill the time with popular songs but I hope Grado is fast because I'm not really a singers ooh that doesn't look good let's not file a book report so I'm on Android studio 2.0 beta 6 that might explain a bit make this a bit smaller see if we can fit everything here so greater still or Android Studios to indexing actually that takes a bit of time once that is done this should also work not sure why that one popped up who by the way is already using Android studio - yeah I like I like the way you dare no it's a lot faster on Android studio - it does have it stability issues of course it's in beta that's the reason for that are you already using instant run when instant run works it's awesome by the way it's really great the problem is that it sometimes doesn't work and then you're wondering what happens okay we're back in the code so this is my layouts I'm going to build a layout for the chat application if you remember from the screenshot we had at the bottom a text box and a button and at the top we had a list of messages so let's build that it's going to be a bit tiny but otherwise things won't fit so I'm quite sure and nobody uses this designer in the way I do but in a demo it sort of works better this way oh let's get rid of that textview so this is going to be my footer so let's call it that we're going to add a button the button is sent so it's a send button oh I expected people to shout out my typos but okay and then we add a text box it's a bit of the tricky one normally it needs to be on the left so this is where I add my text message that I'm going to be sending to the chat so that'll be called message text we're going to make the puter wrap the content then we're going to add a list box ListView so of course in real Android projects nowadays you would use a recycler view but it requires five lines more code and that just doesn't make it fit on my screen so for the demo I'm going to use the ListView like this and in here I'm going to show the messages so let's call it messages list oh that's all we need for a layout next thing you would probably do with dagger in this case I'm actually going to type these Oh that's my send button yeah it's going to complain about that and then offer it by the way I said I was going to not copy anything I rely on auto-completion a lot yeah Android studio message text is what I called it I think that's this one and then we had a ListView with the messages almost done then we'll get to the interesting part okay good so remember that I showed you the jet application that we're building you see it here on the Left I'm going to make it slightly smaller so that we can see it's slightly better I hope so we see that we have a list of chat messages here in this case it's a list with only one chat message in there at the moment and the check message has two properties it has a property name and appropriate message so the first thing I'm going to going to do is since Java doesn't have any way to represent this JSON data I'm going to create a class that represents it for me so I'm going to create a tiny class that has two properties name and message if you've ever done javabeans this is going to be quite familiar we're going to generate a Java Bean plans for this let's call the check message and it has two properties it's important that we spelled in them the exact same way as they are in the JSON because that way firebase can automatically map them from the adjacent to the Java class so I had a string name and a string message now it needs to be a Java Bean which means that it needs a empty constructor let's do that first then it needs a constructor with two with both parameters that's not easy for a Java Bean that's needed for my own code and then it needs together so let's generate those cogeneration is fun okay so this class now represents a chat message in my from the database in my Android application let's go back here it's about time that we start using firebase here so to enable firebase we open the module settings and in there there's these developer surfaces nowadays we have clouds and there are so firebase checkbox this does two things that you can just as easily do them by hands by the way so it adds a dependency to my Gradle file and it adds the internet permission so that's all that we did there are some more instructions that we need to follow so let's open those I'm going to click OK here while that is compiling let's look at the instructions so if your hand didn't go up when I asked if you have a firebase account if you want to take the codelab go there now and sign up it doesn't take anything you need a Google account we don't need credit card details you don't need to pay us you just need to have an account with us so that you have a database so I already have one so I'm going to skip that step now apparently I need to fix the Gradle build we're working on actually fixing this in the actual code itself let's go here I need the Gradle file after all I'm going to add this here I had something weird yesterday with a build problem so what I'm going to change is I'm going to actually change to build version 23 I didn't have time to fix it so I'm going to do that now okay well that is synchronizing we're going to go back here let's have a quick look at the documentation again so we fixed the Gradle build next step is that we're going to set up firebase on Android I lied a bit when I said I did I'm not going to copy/paste anything because this one I'm going to copy/paste frankly we're still synchronizing it should almost be done there we go so this initializes firebase for use in this Android app it tells firebase where to ruin essentially that's all after that the instructions say that we're going to read the write to the firebase database I'm going to type this myself I'm not that lazy so we need a connection to the database we're going to do that by creating a new firebase class and we typically call these ref for reference so this is a reference to data in the firebase cloud new firebase if you've done anything with databases you're probably use that you need used to having a connection spring right I mean this might be a sequel I'd paths there might be a DB C or JDBC connection strings in the firebase case they are a URL if I take to your L I paste it in here so this contains everything that you might be used from other projects this is your API key it is your your project ID it's all in one go essentially now notice everything that we need to know for this my database is called nano chat yours will be called something different the debt creation connection to the database then we're pretty much ready to start sending a chat message so I have to send button and if I now set an onclicklistener to that I can start sending a chat message to firebase remember the class that we created let's say that we do new chat message and that this takes two parameters one of them is the name I'm going to hard-code the name if you take the code by up to 30 you're going to actually replace this with a real logged in user but for the demo now I'm going to hard code it then I need to check message so that's in message txt the get text let's do a two string on that now I need to send it to firebase remember we have a list of check messages we are connected to this place in the list so what I'm going to do is I'm going to add a new TechNet check message then send the data to it so to add a new message we can do revved-up push if you've ever used a raisin in JavaScript very similar and then I'm going to set the value there to my check message that I just created ref needs to be final this is all that it takes to set data to firebase if you've ever done any networking code on Android it has never been this simple I'm not checking for network connectivity I'm not handling connectivity loss yes this will work I'm going to do one more thing just because I think it helps me is I'm going to clear the text box that's mostly so that I know that I actually clicked the send button okay let's compile this and see if it works I'm gonna run it on my emulator this is Gradle so it takes a while to spin up it shouldn't be too bad but I'm running this on a Mac Book Air so if you have a a better machine it probably will be left faster for me normally takes about a minute to run this what we'll have done is that we have the sending of messages from my android app to a database like I said if you've ever seen an API that makes this simpler please drop by after my talk I want to hear about that API because we'd love to make it even simpler so so what we're going to be doing is we're starting the end with the chat app this is still the slide by the way in the background we're going to start a chat app and we're going to ascend a name and message to the database it's almost done it's now installing I like having them side by side that's like you make the emulator a bit bigger this is by the way a 2.0 emulator which is probably the reason my machine crash this morning but it's also nice because I can finally resize it by dragging I'm quite sure Chet and a row man got huge applause for that this morning okay the app started so now we can say hello from Androids if we hit Send now it shows up in the database automatically I'll hold for a pause at this moment thank you wow you guys are nicely following along with that one thanks what I have a time poem yeah I do actually true just like this morning in my tweet apparently but so this is sending from firebase now let's actually get the messages from from the database into the Android app we're good on time so I'm going to do this the hard way so we're back in our codes we have a ListView we need to populate the ListView with data from firebase I'm not sure how you populate list fuse but normally this requires that you use an adapter so let's create an array adapter of check messages of course and then apparently I need to fill in this constructor let's do that first so this takes a few parameters it takes your context that's this it takes your layouts so I'm not copy-pasting I am secretly stealing this from androids build built-in layouts I'm not sure if you know but there's a few layouts built in they've been deprecated since API level 16 but I still use them so this is a two line list item it has two text views then it needs to know the messages that it's going to display so I'm going to have to populate a list of messages some way from firebase let's do that like this those are the arguments of course I don't have the messages yet so I'm actually going to create a local variable and it's going to be a list of chat messages like that we got that I'm not going to populate it yet right there's nothing that takes them from firebase let's first actually make sure that the messages show up in my in my view so I'm going to override a few things like a I'm gonna override one method get few the reason we had to explain this to a teammate of mine and it was very confusing for him that you explicitly have to override get few but it is needed though okay we get a few things here we get the position that we need to render and we get the view that we need to render it into now that view might of course not be existing yet so in that case we need to create it so view is get layout inflator button plate then again the same view as we had before these are few more things needs to be to the parent that needs to be detached from everything else so now we have a view where we can populate it so to populate a view we're going to find the children and still sensitive to a very hope a very generic view they're called text one and text two not very good in a real application but this is what we need here okay what am I going to set in here I need a chat message right remember I have the list of messages so what I'm going to do is I'm going to get the chat message from that list of messages and it's at position so I'm going to use the position there Oh then we're going to display the name hope like that I'm going to duplicate this line and then text to is going to be the message this makes sense okay and I need to return the view and I need to bind the adapter to the ListView I always forget these steps on quite proud that I remembered at this time oh of course I make up for that by making another mistake this is pretty much it what it takes to get data from firebase into an app hold on we didn't yet get the data from firebase yet so let's do that next so firebase allows me to listen for data so firebase write for other database you might be used to a request response model you fire a query against the database it returns the data from the database firebase is really different we synchronize the data remember that animation that I had that means that I need to keep listening for changes to the data so firebase doesn't work with a get data and you're waiting for the response it works with a listener sorry I have to firebase reference I'm going to add a listener and in this case I'm going to add a child listener which means that I get notified whenever a child is added or removed under chat so when I add a new chat message the firebase SDK is going to tell me about that new chat message let's do that I just generated a lot of code for me there's all kinds of events that can happen in this case I'm only going to worry about on child edit for the moment so on child edit I get a snapshot of the data at location of this fires for both the initial messages and for any chat message I add later so this is great because for my chat application it means that when I first start the app it's going to connect to firebase and it's going to get to on child edits because I had to check messages straightaway then when I add a new message it's going to get the next one so and I get a snapshot of the data at that location for the new chat message so let's actually get to check message out of that so we have get value and then I'm going to say give me the value for a chat message remember we just sent a chat message to firebase database and then firebase took the properties from our Java Bean and post them to the database now we're getting it back and then I'm going to add it to the list I need to do one more thing I need to tell the adapter that the data has changed I do it like this this is what we need now of course we have these other methods I'm going to ignore them for the moment and I'll show you why that's a bad idea in a minute let's start this on the emulator again we have two chat messages we have the emulator radio is still spinning so nothing's showing yet but it should be done quickly it's bouncing there I hope it's not showing error messages because that's sort of messes up the demo normally I did yoy Android studio can you not autocomplete a semicolon let's try that again Wow does anyone notice is it spinning is it doing anything yeah okay it's happening a number you like showing the emulator straight away because then you can see that the application loads and it connects to the network and it gets the data from there so okay so now we have to check messages so David wants to give it a random round of applause for that and since David is is our our in-house YouTube celebrity we really must listen to him okay this is awesome because now I can say hello again and I can send it and it shows up on here but it'll show shop in my application straight away let's have a quick look at something because chat is not fun if you're in your own so what I'm going to do is I'm going to open the developer tools here I'm going to do the exact same thing that we had before but I'm going to do it from JavaScript so I'm going to create a reference to the new firebase database or to this firebase database remember that it's a URL so I'm going to do it like that exact same code as we had in Android except that it says bar instead of firebase now remember we add a message so we're going to go revved up push dot set this case not all set value it's called set I'm going to send a name or set a name let's call the user chrome and the message is going to message it's going to be hello from Chrome now if we hit enter the message will be added at the top and the clothes we added my fire to my Android app so with this we have a cross-platform multi-user chat application okay I didn't build a GUI I didn't really stuff my JavaScript into a file yet but the code is there one thing I notice is that it only just fits I don't sort of like that so let's see what happens if I remove a chat message let's remove this one so now message gets removed from the database a chosen read hmm but it doesn't have to be moved from the Android tab who knows why goods you're following along I like that yeah I didn't implement on child removes you notice that there's a lot more going on here we have on child change on child move we have this extra argument that we're ignoring it's a lot of work to make this work so what we've done is we created a library that handles most of this for you the library is called firebase UI and we're going to replace most of the code with that so let's go to my module set into then let's add a dependency again you probably do this in your Gradle file I do it through the GUI for now so firebase UI is the library we're on 0 3 1 this is a mobile binding library so it simplifies the work I've showed you how to do this manually if you feel like it with your own code now I'm going to do it automated so great was synchronizing we're going to get rid of most of the code that we just wrote so this own child listener thing it's not needed anymore so let's get it out of there then we have this adapter that we display here I actually don't need that for the most part what I do need is these two lines as we still need to populate a view so let's cut those two lines let's remove everything else I still need to set an adaptor and I still need an adaptor of course so what I have is a firebase list adapter this comes from the firebase UI library it's of course a list adapter for check messages so almost the same as before except that it auto populated a method for me I still need to populate my constructor parameters there pretty much the same as before but slightly more so I need an activity I need a model class so that's check message oh that explains what's class I need a layout remember we were using androids r- item and I need a reference and I can just type ref in here which is all the firebase data at that location but to make it slightly more fun I'm not only going to show recent messages I still need to create this I quickly want to do one thing because otherwise I'll lose my clip words I'm going to paste the code in here it's the exact same code that we had before but now that's the only thing we need here so let's show only recent messages so before we were showing all the messages now I'm going to show only the recent messages of which is a firebase query and it's actually based on the reference that we already had but now I'm just going to say limit to last three because that's what I could reasonably fit on the screen here but this means that this firebase list adapter will synchronize three messages from the server so right now we had three messages I think so it will show all three if we add a fourth message it will automatically remove the first one if I actually remove the last message again it will react that one back this is it this is all we need to do a chat application with firebase and firebase UI if I use the slightly smaller font it could fit on a single screen let's see if this runs it seems to be doing okay with that so it's going to increase your to start my application out right now the result should be Excel actually we already had four MS know that's what still the fourth message that shouldn't be showing I was getting concerned there so it should show the three messages that we had before nothing changed except that I threw about half of my code away the code that was difficult but now I can send a message again if I send it now it's a few things we have a fourth message here now but my first message disappeared from here let's handled automatically for you because when the new message is added we get an uncharted for the new message we get an uncharted for the previously first message that we now need to remove so everything happens automatically for us because firebase list adapter handles that for us if you want to see the firebase UI project it's open source you can just see what we do there by all means copy the code it's there for you to use it's sometimes tricky to get that code right that's why we wrote it for you this also means that if I can now remove a message from here that it will update on the right and we get our message back everything happens automatically you can do fun little things with that I lied a bit I did to a snippet just there you can reorder these messages that means that are now being reordered dynamically and it's also updating in my Android app it's a great way to unit test code we found out by the way okay it's an androids chat app in let's see probably about 50 lines of code it's as easy as we can make it right so you had to surfer through the code now we're back to slides and we all know how you like slides so if you like this oh I forgot one thing we handle offline very well remember when I said you don't have to detect if there's a network connection you also don't have to detect if the network connection disappears so firebase keeps in memory a cache of all the data you've already gotten from the server if you send a new chat message it will show up in your screen straight away but it will actually be fused so if there's no network connection it will be queued for writing to the server so that means that we keep everything in memory when you come out of that tunnel it's automatically synchronized to the server and synchronize to all other connected clients so you don't have to write any code for that the code we just wrote already does that for you what it doesn't do yet is disk persistence so what I just said is like the app stays active we do have a command that writes it to disk it's a one-liner that you adds then we save the data to disk also and that means that you can also handle airplane mode all of that is literally doable within what I just showed 40 minutes that's the real time database need to cover a few topics yet cuz nothing I showed you so far it was secured I have a few validation rules on this database I didn't sign in that means that any one of you could have gone into my database and add the check messages normally when I do a web version of this somebody does that's not what you want so you need to secure your data and to do that you need to know who your users are so that's firebase authentication that's our second big feature not going to spend too much time on it but we have three types of authentication we do email passwords and if you go to our code lab you're going to wire that one up we do social providers so you see them here Google Facebook the usual ones and we do a custom back-end so if you already have a database of users you can use your user database authenticate user and tell us about it then allows you to secure the data so the authentication is handled by us and then you can authorize that user to access read or write specific parts of the database that's how you secure firebase back-end on the web this is trivial on Android this used to be quite difficult but we recently released a new version of our firebase UI library that includes the UI for signing in with all these providers so you see it here the screenshot on the left is from Android to one of the writers from iOS we have this library for both Android and iOS they do the same and so you can see that we handle all the social providers all you need to do is provide us with the keys you enter them into the firebase dashboard that we saw earlier and then you're good to go with this again the code lab is going to talk you through that yeah a quick bit about authentication so this is the code that you would need if you're not using the library its authenticating your user with the password who knows Bob tawny and correct worst battery staple who knows what that's a reference to tell me yeah it's from xkcd where it explains that a long password nowadays is better than a complex password so by the way a long password is better this long password is probably the worst one you could pick because I'm pretty sure it's right in all the rainbow tables after secret passwords and something else so this is how you sign in two things can happen when you sign in this is on androids it either succeeds or it doesn't if it succeeds you get a little bit of auth data and in there we have two UID the user ID that means that we can now identify Bob Tony that's great because that means that we can also make sure that Bob Tony can only post to chat rooms that he's member from we're stuff like that and good thing there is that this UID is going to be persistent so if work Tony synes out signs in again it's going to get the same UID if he installs it on a different Android device same UID or even with the web application or an iOS application Bob Tony will still be identifies as the same Bob Tony that's firebase authentication we also do static asset hosting which is great if you're doing web development but we don't because we do Android so these are the three main firebase features real time database user authentication aesthetic hosting if you like this app if you want to build an app like this keep in mind nothing I showed you today from firebase is chance it's just a database I happen to model a chat application in it if you like that go to the code level you also built that same chat application it takes about 40 to 50 minutes you will get something working by the end of that time I guarantee it the only thing you need is Android studio if you don't have that installed yet you have about 20 minutes and the Wi-Fi here is pretty good so you should be good by the time we start the codelab ape is actually teaching that code lab but I don't see them right now oh he's sitting over there relaxing so let's go but that's just one type of application since this is just all my database whatever your mind can dream up you can build on firebase if you've taken the code like I want to learn more we've released two months ago a course about about firebase on Udacity and it's firebase essentials for Android it's free like all Udacity courses you can take the course for free it's about five lessons and it takes you through everything you need to know to build an app for four androids with firebase so they're building a collaborative shopping list application so another chat app and that means that users sign into the app they create a shopping list invite their roommates to also be on the shopping list they add items to the shopping list go to the store check them off right and just like with everything else in firebase whenever is one of them makes a change all those changes are synchronized to all other connected devices within milliseconds quite important so you see that we have two data tree on the left here that data tree is a bit more complex than what you've seen before so they also teach you know sequel data modelling it's fun they actually make a mistake in Lesson one that they then fix in lesson three and I make a mistake in lesson three that we tell you at the end it you really shouldn't be doing but we don't fix it so but it's a great course it's set to take about 40 hours because you are going to be programming the application so at the end of this you have you will have built so you will need to know Android development already so the Android development course is sort of required for this but at the end of this you will have built a complete app that actually could just go into the Play Store I expect a lot of shopping list applications in the Play Store in the next year it's taught by the Udacity teacher so the girl with the green hats on the couch is Lila she did Android beginners and she also does this one the guy with the red head is JP he's a Googler he explains most of firebase and he's really good at explaining firebase he also talks a lot slower than I do so that's good like I said they did explain no sequel it's for free if you just search firebase on Udacity you're going to find it usually to the codelab so remember to go there it's in 20 minutes it's in the code lab space over there we have tons of space we have lots of people available to help you with love it if you show up and write a chat application with us that's pretty much all I have so if you have any questions just ask me I have probably about 5 to 10 minutes for this I'm checking up some of these signaling that otherwise you can always reach me on Twitter I puff or you can follow us on firebase I have some short links here but to be honest most of us most of our stuff is easily findable if you google for it so any questions tell me oh love the question let me repeat it so um firebase handles offline and right when you come back online we synchronize everything automatically and in chat it's important that the chat messages arrive in the correct order and they do so I'm not sure if you notice we had that very weird long key in there it started with - and then Kay those are not your regular array indices those are firebase push IDs and the push IDs are generated on the client and they allow us to do two things one if we would use array indices then if we are chatting and we had three messages in there and we are both typing and whoever hits send first it's going to be the fourth message right and then other one is going to be the fifth so we need to agree on the number of messages which is horrible for concurrency right this is going to be a contention issue so that's one of the reasons we don't use array indices the other one is that array indices would never work offline so this key is generated client-side that consists of the timestamp and some random junk to make it unique it's essentially but like a UUID except that it's ordered and the timestamp is corrected for your offsets to the server when you were last on line and that means that we actually have a pretty good idea who hit enter first even when you're not on line so when you get out of that tunnel the messages are still merged together in the right order this is pretty important with firebase we tend to recommend conflict avoidance and not conflict resolution this is conflict avoidance essentially these keys are guaranteed to be unique okay we quickly check the clock before ah we're good good question let me quickly repeat so the question is how do we handle if you are adding properties to the database on the web this is easy because Jason is very relaxed about these things on Android this is slightly more difficult so API versioning as this is known is something you definitely need to consider what you would do here is I do need to migrate the data over which is just a matter of looping through it and adding some value for it or you need to handle it relaxed Lee in your Java Bean so you can add annotations there that say ignore those properties or write them back but we are this is something we are aware of and we are constantly looking at ways to improve this because in Android and iOS this is more difficult than it is on the web so a few hands going up here and first critical Oh over there apparently tell me now that that great question so the question is if we have support for push notifications for when the app is not active so we don't have support for that built into the into firebase I'm actually quickly going to run up so I can show you something so we work only when the app is active or when you put us in a service so we need a connection to the firebase servers so we keep a socket open to the server and that's the way we can get that instant feedback and that's of course great for when the app is active you could even put this in the surface then it runs in the background but your users are not going to be happy because we're going to drain the battery so that's not a good idea but we don't have anything built-in if you look to tabs to the left you're going to see sending who's notifications I'm working on a blog post that describes how to do this it should be out soon but I sort of got distracted by preparing for this talk so I'm going to come down again start asking three yeah yeah so what I just showed you sorry so the question is how do you handle if you have multiple users and how do you talk to them so it's a typical scenario where you have multiple chat rooms so what I just showed you is there was one chat room but when I showed you the database there were three different top level nodes i chat DP g burg bash and messages those are actually three different chat rooms essentially they use a slightly different format so you will model the users into chat rooms so either by doing obtain chat rooms like named chat rooms that you would know from slack or something like that or you could even model one-on-one chat rooms for example we see that quite a bit remember that we have to user ID if you and I are chatting then I could just concatenate your a my user ID together and I'd get a key that uniquely identifies that it's us chatting and it's repeatable so I don't need to do any weird querying for it as long as you and I get the same UID we're going to be in the same room that's how we recommend modeling a user to user chat rooms our timing is great guys okay I'm gonna wrap up here because we're sort of out of time I do have time for your question but that means that I'm gonna kick everyone else out it lets with you okay thanks for your time thanks for your attention again go to our code lab it's going to be fun we're going to have tons of fun over there
Info
Channel: Touchlab
Views: 157,378
Rating: 4.8482647 out of 5
Keywords: android, development, design, mobile, web
Id: Yt75ZIGdJ4Y
Channel Id: undefined
Length: 44min 29sec (2669 seconds)
Published: Thu Apr 28 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.