Databases for Mobile Apps: React Native & Realm

Subtitles
Show
stop that react native EU is back for an on-site addition write that down September 7th and 8th in vrodsworth you can expect a industry thought leaders insightful talks networking with great deaths from all around the world and of course Good Vibes only write that down September 7th and 8th but also have get your tickets now do it hello everyone and welcome to the react native Show podcast uh today we're gonna talk about databases in mobile applications so to do that I invited Andrew Meyer who is a ex stuntman and software engineer from mongodb and we'll be talking about Rel so uh Andrew hello welcome to the show can you please introduce yourself yeah hello uh I'm Andrew Meyer as we've heard uh I'm a senior software engineer at mongodb I've been there for about two and a half years um I was hired on the realm.
js team uh to basically do uh the react native integration so classically realm.
js was developed for node applications um it's kind of like a server language and uh people are having troubles using it in react native so uh I've been helping with the uh the react native integration and yeah awesome I classically come from a react native background um before before I was hired I was a react native developer since about 2017 so uh yeah somewhere around react native version 35 or something like that oh yeah okay so that that's quite a long time ago the 35.
was part of the whole equation yeah yeah yeah can you can you please maybe explain to me as a newbie uh in like the databases environment what's the difference between like mongodb and Realm well mongodb is uh is a server database like uh application so you're basically running a server uh basically running
js team uh to basically do uh the react native integration so classically realm.
js was developed for node applications um it's kind of like a server language and uh people are having troubles using it in react native so uh I've been helping with the uh the react native integration and yeah awesome I classically come from a react native background um before before I was hired I was a react native developer since about 2017 so uh yeah somewhere around react native version 35 or something like that oh yeah okay so that that's quite a long time ago the 35.
was part of the whole equation yeah yeah yeah can you can you please maybe explain to me as a newbie uh in like the databases environment what's the difference between like mongodb and Realm well mongodb is uh is a server database like uh application so you're basically running a server uh basically running
the service on the server that's that's listening to multiple connections it's uh we're handling a lot of uh concurrency and realm is actually a local database on your phone so it's uh basically only just handling that single user at one time and um also between the two differences uh how how you access the data like in in mongodb you use queries uh and and you know get some Json back whereas realm is integrated within your language it's an object store so you basically get objects back you get they look like JavaScript objects but they're actually realm objects because you if you access them it's actually running C plus code that that we hand crafted so it's uh super performant super fast and uh you don't really feel like you're working with a database it just feels like you're working with Native data structures so it's kind of a difference or Mobility B it's yeah I guess we'll be talking about like Realm functionalities and certain around technology in the later parts of the episode so what I want to start this episode strum is to briefly go over like the data in Mobile in general so not only databases but also like data structures and the way we handle persistence data so can you tell us something about the history of uh databases in Mobile before we had Android and before we had uh iOS there was Nokia right right and yeah Nokia uh had their own um I think it's called like Series 30 and uh phones and they they didn't really have a database because we didn't have applications back then so they weren't you basically just have these like little little apps you would run right there'd be like you know storage of contacts and stuff like that but it wouldn't have like a classic database
the way you would think about it today I wasn't until their their later models that they actually started having like SQL light involved so if you're just gonna run you know snake on your phone you don't really need a database right but it wasn't until the uh the application space started where we could really we really needed some more data intensive applications and sqlite was already uh uh developed and already in usage in Nokia forms uh it wasn't it didn't get there for a while actually like sqlite was originally developed as an embedded uh system database for battleships actually I think for like missile targeting systems and so yeah there wasn't really anything uh available at the time that was just running locally on a uh on a single uh chip that that someone could access most databases ran in mainframes so um it was uh originally developed so that they could run it you know basically on a single application um and then it was open sourced um and picked up by the Android team uh as basically the database that that Android applications are going to use and then started becoming the the mobile uh database that exists on Android phones so it was pre-baked into the phone itself iOS devices as well by now this is this is also my knowledge that uh iOS devices and Android devices have that sqlite integration already there already uh yeah available and iOS also had another database that was called core data uh this was actually a super Antiquated uh system out of the next OS era like way way back when um you know next is uh the X and next is where the X and X code comes from so that's like how far you're going back oh really okay free Macintosh right so uh yeah uh core data I guess from what I've
heard I've never used it but I've heard that it was extremely difficult to uh to work with like if you got it wrong it would just crash and um I think we have Objective C from the same reason that it will it used to be in next operating system and then it got it got picked up by IOS as well right right yeah we're just like Mac Mac apps in general like Macintosh apps in general so like that whole yeah so what is the story uh going from like this embedded database sqlite that you have already on Android and for in an iPhone what was the reason that realm was created then so uh I was talked to the founder Alexander styxon and uh or sticks and sorry he uh actually was working at Nokia and he left the company and wanted to make a uh his own his own company where he's developing a code editor uh that was collaborative kind of like Google Docs now and so he was trying to figure out a way to do that uh using the tools like sqlite and things like that because even if you're making a desktop application it's still a local database doesn't have to be on a phone so uh but he was having like you know thousands of crazy relations as you're dealing with every single change that occurs and all these change sets everybody's finding that SQL light wasn't performing and so we decided to uh boldly just try and develop his own database um and then he realized like this could actually be the company that uh that uh he he could be starting and so he was working on that and decided in in 2011 to just try apply for y combinator and uh they got accepted and uh they moved to San Francisco it was they were originally in Copenhagen and basically started realm database um and I think they got picked up like started gaining popularity around the time Swift was released
they had originally uh like the the big projects that I worked on why combinator was like an objective-c version of Realm and uh as soon as they were about to to release uh the WWDC just like announced that they're releasing this new Swift language so they had to quickly uh panic and adapt and so that yeah so yeah and one interesting bit of information there is that at the same year that realm was in the Y combinator another very popular solution was in the same batch as well right right exactly so uh Firebase was also an Alum that year so uh Alexander was working with the the owners of Firebase at the same time collaborating on databases in the in the mobile space so yeah yeah and they both kind of branched off in their own different ways as we can see yeah yeah we will make some comparison to Firebase and uh realm in the uh in the end of this episode so uh going back to the like initial question of why do I never even need a database uh I'm a react native developer and like I'm completely satisfied with the options that I have the options that are really easy and I don't have to um add any additional package to get so what I have right now is essing storage which sorry it's not true I do have to add that package it's no longer in the car but like this is very like Plug and Play Right async Storage uh which you could call a very very simple database according to right like async storage is a key value store so it's a very simple database uh it's at the end of the day it's it's just a file right so you need to make a key it's it's saving uh a string and that's it you have different keys for different strings uh in the most case the string is a parsed Json object that you have to stringify to save and parse
to get back into your application so for simple applications that's that's really good but if your local data is significant uh it could take some time to uh to load that and hydrate it and also to keep track of it right to track the state on it yeah so like going on with your uh like growing example if I have a growing data structure then I opt for Redux or mobex or other State Management libraries and I can uh say persist my data and then hydrate it and I guess under the hood they still use the async storage but then I have some more fine great control over like the shape of my data structures right and that's a that's a valid option I mean it's it's it's legitimate uh but at the end of the day like you have everything in memory at that moment in time right you don't have any and you're constantly just just reading uh your object you're updating your state it's uh you're persisting it like you have you have that whole uh stale data that you can look back on which is kind of nice after a while but if you don't need it you don't need it right you're not cleaning up as often so you're building this this giant state tree and this giant uh memory space when you're using uh the State Management systems and classically that's good like for web that makes sense right you in web you don't have a local database you're getting information usually from a rest API and you need to just store it in the memory of that browser and work with it imagine that state but when you're on an actual like mobile device you can actually you can leverage the hardware that's that's in your hand right and you could actually have a database that stores all this information and accesses in a way that is super performant is you know putting it in memory the way it's supposed to be and not just relying on
the JavaScript runtime itself to to handle the memory um which yeah yeah you do any Research into that you kind of know that JavaScript isn't really super duper efficient when it comes to memory storage um right yeah and what would be so I guess at the end of this ladder we would go we will talk about realm what would be the step between State Management libraries and uh persisting and hydrating those and realm uh I guess the SQL light kind of things what what are those right so you could leverage uh sqlite that's on the phone right now using I mean you could you could just leverage it and write these uh SQL strings yourself and you know but typically people don't want to do that they don't want to write all these SQL statements all day it feels like you know a class you took in college right yeah yeah so um they're uh there are a lot of orms out there so object relational mappings and basically that's uh kind of an object syntax on top of the database so as your your accessing this object that's that's uh thank you kind of modeling the data that you you have on your phone it's automatically under the hood making these SQL queries for you and you know update queries select statements filters and all that under the hood and um that's fine but it's it's kind of a pain like if you for example have like a collection of data with some sub collections then you have to you know create a join table or if you want to have a many many relationship like you have to basically have another table in your database that's just IDs that link things together and if you say have get a a person out of the database and the person has a collection of dogs when you dot dogs
it's they don't exist yet it has to go in query now it has to go to a join under the hood and then populate that for you right so there's a lot of these these queries that are happening um and if you want to do anything uh more complex maybe at the end of the day you still end up having to write these queries yourself like from for the most part or I'm just handle about 95 of what you want to do but if you know that five percent is when you actually have to get under the hood and learn how that database works yeah I wonder also about like uh type safety in this kind of applications and like Dev experience the autocomplete stuff like that how how is that how does that work uh basically you'd have to model that yourself right you have to you have to write your own Types on top of that and hopefully expect that that's what's going to come back from SQL itself um the types aren't generally like you know one-to-one mappings from typescript two to what you have in sqlite um rum also has its own types that don't completely map but uh We've we've created like a class-based model um uh Syntax for Realms so you can model your data in a class uh which directly maps to what realm will be um it's actually we came out with a babo plug-in that kind of reads the class and actually like generates the schema for you it's a it's a bit of a experiment at the moment I'm kind of thinking more in the way of like going in a code generation route where like you define your models and I don't know if you've used graphql before like you know graphql code gen where you can write your graphical queries and then just runs over your code base and then
you get these nice like uh these nice convenience functions you can use or convenience types that are just small enough yeah I have I think that's something we're going to look into the future at the moment you define your things in class you get them typed and what comes out of your database is is typed right when you say give me all the round dot objects if a person you pass that person class in there you get a person back that's typed you get the autocomplete you can see what types it has um at least for most of the orms I've seen uh you basically get your objects with a string saying like the uh the collection I want is called persons and that's a string and then you have to use the generic type that you self-defined to to get that out of there oh okay yeah right so if that all RMS are s-colides so relational databases are under the hood uh how is the realm different how it differs from relational database SQL database right so realm is an object database and uh which basically means you get these objects back we talked about that a little bit earlier yeah but it's also it's not an interface on top of you know an existing database like you know you're at you're accessing realm directly um classically all the the teams that we have in our organization are based on the platform and languages that people develop apps in right so we have a swift team a Java team kotlin team flutter team and they are developing basically a layer on top of the core they're writing in C plus plus so that you get a version of the database that looks exactly as it should in the language that you're using and uses the same paradigms and then you are doing the bridge on top of that and giving us the react native right exactly
so we had a JS version that was exactly you know feels like JS and then uh I created a realm react Library that's basically providing react Hooks and providers so that you can access that data from realm.
js right so with all that said what is the actual benefit I get from this being uh object database the benefit you get is uh ease of use like it's a nice developer experience you don't really have to think about how your data is being stored in an efficient way you can just access it directly and also you get speed out of it as well all right so you you get instead of just having these JavaScript objects that you're working with all the time like you you get memory optimized versions of these objects and we can go in a bit more detail about how that's done um yeah please so like on High level I can see the difference I can see the analogy to to like old architectural react native and new architecture of react native so all architecture is a bridge that needs to send the messages as a Json it needs to be um Parts decoded stuff like that right and the native player then has to like act on those uh things so that's like or or Rems right then I have the SQL statements that needs to be understood and blah blah blah right in your like in this example the realm is new architecture when I have just direct access to C plus plus objects that are my state that are my data right and you also get the benefit that the data that you get isn't a copy of your data it's actually the live data so you're actually seeing the live results um and the way we manage that so that you don't run into issues is that if
js right so with all that said what is the actual benefit I get from this being uh object database the benefit you get is uh ease of use like it's a nice developer experience you don't really have to think about how your data is being stored in an efficient way you can just access it directly and also you get speed out of it as well all right so you you get instead of just having these JavaScript objects that you're working with all the time like you you get memory optimized versions of these objects and we can go in a bit more detail about how that's done um yeah please so like on High level I can see the difference I can see the analogy to to like old architectural react native and new architecture of react native so all architecture is a bridge that needs to send the messages as a Json it needs to be um Parts decoded stuff like that right and the native player then has to like act on those uh things so that's like or or Rems right then I have the SQL statements that needs to be understood and blah blah blah right in your like in this example the realm is new architecture when I have just direct access to C plus plus objects that are my state that are my data right and you also get the benefit that the data that you get isn't a copy of your data it's actually the live data so you're actually seeing the live results um and the way we manage that so that you don't run into issues is that if
you're updating that data we uh we have these write transactions so basically you define a transaction realm.
write you give it a call back and when you're inside of that transaction you're able to modify that data directly and um if you have multiple transactions happening at once uh realm core underneath knows like you know one transaction after the other if you do something that you're not supposed to then it's going to roll that transaction back right and then it's going to invalidate it and your data is going to be fine all right okay so that's something a bit different like normally you know if you're using like a state management and you modify the data directly like well it's only modified for that moment in time but it's not actually saved right if you tried to modify anything that that you get from Redux right um but you would have to go that extra layer you know sending a a request to your state tree to update the data itself but with realm you can just update it directly within there and I guess the next question is how do you get updates then like how do you know that your data updated um we also have event listeners that you can apply to um but you don't have to when you're using realm with react native with the hooks that I've developed I've actually created the event listeners for you which will return an update it'll basically trigger a re-render whenever any of the data updated if it's relevant right okay yeah maybe we can actually like start discussing this part um so and we will go back to the to the nice things and user experience and and all of that as well later but maybe while we are in event listeners can you
write you give it a call back and when you're inside of that transaction you're able to modify that data directly and um if you have multiple transactions happening at once uh realm core underneath knows like you know one transaction after the other if you do something that you're not supposed to then it's going to roll that transaction back right and then it's going to invalidate it and your data is going to be fine all right okay so that's something a bit different like normally you know if you're using like a state management and you modify the data directly like well it's only modified for that moment in time but it's not actually saved right if you tried to modify anything that that you get from Redux right um but you would have to go that extra layer you know sending a a request to your state tree to update the data itself but with realm you can just update it directly within there and I guess the next question is how do you get updates then like how do you know that your data updated um we also have event listeners that you can apply to um but you don't have to when you're using realm with react native with the hooks that I've developed I've actually created the event listeners for you which will return an update it'll basically trigger a re-render whenever any of the data updated if it's relevant right okay yeah maybe we can actually like start discussing this part um so and we will go back to the to the nice things and user experience and and all of that as well later but maybe while we are in event listeners can you
please um try to explain to how how do I start using this how is it built like on react only site what are the paradigms that you are using you mean like uh what you would do to get set up with with realm today like yeah yeah right so like I mean npm install realm and uh at realm slash react those are the two libraries realm itself being the realm.
js SDK and then at realm react being the uh react native layer on top uh and then you would think about how you want to uh what data models do you want to save so you would write your your classes right or your data models basically defining a schema of what you want to do and then um you take the you input the ROM provider in your application to wrap your app and you provide that with um schema array array of schemas right and these schemas are actually that you defined yeah schemas are just the JS classes uh yes there's actually uh if we get down into it there's actually two different ways you can model data like before typescript was huge in classes were kind of big in JavaScript um we just use schemas schema objects themselves so basically it's just an object it has an uh a structure that you have to um adhere to which is a name a name field which is the name of your model and then a properties field which shows you which properties are going to be in that um model right and each property can be like name which is the type of string age which is a type of integer um birthday which is a type of date and then you can even go further like if you you can set up like object links so if you have uh children for a person then you can say okay well I can just
js SDK and then at realm react being the uh react native layer on top uh and then you would think about how you want to uh what data models do you want to save so you would write your your classes right or your data models basically defining a schema of what you want to do and then um you take the you input the ROM provider in your application to wrap your app and you provide that with um schema array array of schemas right and these schemas are actually that you defined yeah schemas are just the JS classes uh yes there's actually uh if we get down into it there's actually two different ways you can model data like before typescript was huge in classes were kind of big in JavaScript um we just use schemas schema objects themselves so basically it's just an object it has an uh a structure that you have to um adhere to which is a name a name field which is the name of your model and then a properties field which shows you which properties are going to be in that um model right and each property can be like name which is the type of string age which is a type of integer um birthday which is a type of date and then you can even go further like if you you can set up like object links so if you have uh children for a person then you can say okay well I can just
make another person that it links to and then so you can even self-reference uh the same model within the model okay or dogs you can have a dog object and have that also represented that class so and then after you have those schemas defined then uh you you wrap your app at the realm provider and when you want to use that data within your app then you import use Query out of Realm uh you pass that in that class that you want to get out of it and then it will return the collection of uh of data okay all right and then yeah yeah please continue right uh and if you want to filter that um since the last version I just released uh usually you would just call uh dot filter down that and wrap it and use memo or something like that but uh I created a second argument to use Query which is a callback where uh you get a access to that collection that you um that you want and then you can call Dot filtered within that callback and then you get a memoized uh collection back so it's just gonna uh you can even pass into dependency array if you want to like sorry let me go back to dot filtered um dot filter has like a query uh syntax like age greater than five you know give me all the persons with age greater than five so you put that in there and now you're gonna have a collection that's always going to have all the people that with their age greater than five right no okay okay and then I was before you said that I was going to say oh okay so this is almost like selectors in Redux so that I get my data and then I can speak something else from it as well but then with the introduction of this like uh conditions right you can like actually select data more more precisely
exactly and uh what's kind of nice is you can call sorted as well uh which can Define how you sort that data and imagine like you're displaying a list of people and your UI has a way to filter and sort so if you have a use State up above that's basically keeping track of what you selected for filtered values then you can use that state as a dependency array into use Query so that it will rerun that uh that query when those objects update say that age is a is a scroll uh and you need to select which age you want like and you move it to 10 then it's going to re-update that query and say okay we actually want people that are greater than h10 and we want to sort it alphabetically from you know descending yeah and yeah that could change as well and um that happens really fast it's like it doesn't need to make an extra you know SQL query when it does that it's just modifying that that internal query and you'll get the results back um so I want to touch on something that we discussed uh with you and some other people from your company from Realm I I could say Monaco to be your company but yeah uh that I've heard this nice phrase that mobile is uh ultimate distributed database and on top of that you have just released um this new feature of synchronization between uh yeah maybe maybe you can say that what's the feature is and how it connects with this like distributed database right so since realm's uh marriage to mongodb we've been uh we have access to all their their Enterprise uh uh or their uh server offerings right um the atlas app Services uh and with that you're able to have a global database that actually can communicate with that local database so each each of these applications that are out uh are basically having a copy of that that
um that Global database that mongodb offers and synchronizes with it right so now you have a basically a distributed system of uh many different mobile devices out in the field that have various network connectivity issues or maybe they're offline they're able to access their data they're coming back online now they need to say what's changed what's different and [Music] um basically the uh the server will handle that for you and actually be able to send change sets from from other pieces of that data and be able to come back and have the exact um reality of what the world is in that moment right so um moving on to that so that's basically what we like to call an offline first uh architecture so um your data when you have a local database you'll always be able to access your data when you're offline you know if you're very first time open up the app um you have nothing right so you have to download the the data that you need at that moment in time um and we have a a trip for that it's called flexible sync basically you subscribe to a uh a subset of that data that your app needs typically if you in most apps it would be just your user ID is probably what you want to filter your data on when you authorize right and then you you can start you know working on your data and after that's done then you basically have this offline first app you can edit your data at any time so Andrew from what I hear about the synchronization this offline first part is that I could theoretically have my database uh be a substitute to my like API calls
to have only data like being populated through database and not through uh calling the API right so like I like to think about it so In classical you know uh architectures uh you know for like web applications or any other mobile applications right you usually making rest API calls to an API and caching it caching the result if you need to update things then you have to make a request a post request to update that data and then you have to do another request to get the data back you also have to set up a probably some sort of subscription like a websocket or something or some sort of polling to to keep getting the most updated version of that data um but when you're using realm with sync it's basically like your database becomes your rest API right so as long as you're setting up a listener to that collection of data you're always getting as long as you have a network activity you're always getting the most updated thing you know it's already subscribed to to changes and you get updates when those things change and if you write to it it's automatically going to update what's in the cloud so yeah the like the challenging example that I can think of when it's quite hard to synchronize is chat applications right because like you need it to be like Fast reliable all of that and like on time and like I feel if someone takes the burden from me of doing the hard work of synchronization and I can just focus on like creating the UI and it's gonna work like this could be a very good use case to like not do the API completely and just rely on the database underneath right exactly so we just take all the hard work out of it yeah you could basically use something
like flexible sync to apply to a chat room and then you know then you just get the subset of data from that chat room so you could easily have multiple chat rooms running against your your mongodb cluster um but your mobile app that's that's currently subscribed to that chat room will only see the changes it's interested in and that should happen quite fast yeah so I think that in the beginning of the episode that we will compare the Firebase and realm so let's do that now let's maybe um have that comparison how is what I can do with realm and what I can do with Firebase what are the differences there in possibilities they're definitely very similar in uh terms of what you can do right uh Firebase off let's talk about firestore I'll talk real quick there's two things that Firebase offers they have the real-time database solution and the firestore solution uh I would recommend if you're going to use Firebase look at the firestore solution well the realtame database is uh it's basically just storing Json at the end of the day right it's just an object and firestore you can you know set up Collections and sub collections um but what is the difference between Firebase and realm they both offer synchronization they both offer queries you get your updates but Firebase is uh when it comes to local right um I would say Firebase is more remote first whereas realm is more local first right um you are able to work on your data in Firebase when you're offline it will store all the transactions you're you're working on as you're as you're making updates to it but it I was just reading through the manual at some point like if you if
you're setting up a really complex transaction uh and you restart your app you will lose that those changes that you just made so um and realm it's a bit different like you we're also like you're making all these changes to your database we're also keeping a log a change log that's going on but we're not going to wipe that out for you like your local database will will be be the same there are situations of course where you can get in this Stitch uh the situation that like this for instance if your app's been offline for a very long time and the schema has changed on your on your mongodb data like the structure has changed there's like no way you're going to be able to uh to migrate um then basically at that point you can just wipe the data on the phone and get the uh the latest changes downloaded right something that I picked up from our conversation previously is that it works similarly to uh git right uh when yeah yeah when you have conflicts you have to resolve them sometimes it is not like you said just now sometimes it's not possible to resolve the conflicts but you can have your everyone has this distributed version of called um you can make your changes locally and eventually after you push resolve conflicts it will be what's the word consistent on eventual consistency it's called right yeah so that yeah so that as you're working on that copy of your data and git at some point you're going to want to you know merge those with the changes that are up there and you do that in the local you know you could obviously just say from your client Force push and override all the changes your your teammates made but I'm sure they they're not going to like you after that
sure so yeah it's very similar the same type of problems uh that you have would have to solve with merge conflicts is kind of what we're solving for you with uh with realm data and uh in a cloud database okay so maybe talk let's talk a little bit about the use cases we touched on the very simple solution like icing storage um and like statement libraries but then what is the the the this step what are the use cases that you would Advocate to use more advanced solution like like Realm I think any even simple apps like could benefit from using realm as you know if you ever need to do anything especially with you know realm and sync if you ever need to do anything with a back end and you're setting up you know an API and a back-end server and a database thinking about distribution and things like um it's nice to just use realm right away uh because then you don't have to like think about these optimization things right you know it's not hard to get started with realm and you know like there's a classic like you know don't pre-optimize your code because you'll never get to release well if you just start using realm and sync right away like you're basically pre-optimizing without wasting any time so that's definitely uh well if we'll move on to the next Point like what are what are specifically good applications for realm um if you're in areas with low latency um say if you're creating a logistics app and you have uh truck drivers uh using a mobile app to know like where they have to you know deliver their packages to um you don't want them you know being out in the middle of the country trying to
see okay where's that address and all of a sudden they get a loading spinner or at the same point they want to report right away that they uh their job is done and that they've delivered their package and um if they can do that with Rel they can simply modify that data and as soon as their mobile device gets Network again it's going to send and update that change set so that the people in the logistics Center can actually see oh okay they're done with their delivery right um it's fun we actually even had a a GitHub user um filing uh issues for us uh from uh who's actually a farmer in Africa and he's like making apps for farming you know Africa typically doesn't have very good low latency and so he's able to actually do some tracking of his uh his farm farming operation using realm and sync so okay so and so basically uh it's from what I from what I'm hearing is that it's good for everything but especially good for like um Locker first database which it is uh for low Network conditions stuff like that right yeah it's also nice like if you if you do want to have you know just the local part around like if you have an application that's like a note taking application or anything like that um it's nice to use realm because you your data stored in kind of more of a database instead of just a giant file like it'll it'll take up list space on your disk um yeah if you're right so I was going to also ask because we already discussed how to set it up in my project but how do I set up the synchronization do I need my own server with mongodb or do you have some kind of maybe pre-made solution that I can hook into uh there is a little bit of a setup uh
we're trying to make it easier and easier but you do we have a we have a free tier for sync like uh sync itself isn't isn't free but after you uh you scale to a certain point you do need to set up an atlas um cluster um so basically you can make an account on on mongodb and and set up an atlas and that you deploy cluster and then you create it basically it's under app Services you create a an application and that is what you can use to start sync we do have some easier ways to get started like if you um we're going to share some links uh uh the getting started tutorials um which will deploy basically like a to-do list app and automatically set up like your Atlas and app services for you and I'm wanting to like make a command line tool in the future where you can basically just say like react uh realm in it and then it will just set up that background for you um but yeah so let's take that back a little bit so once you get set up with app Services um you basically create a realm app you set up authorization of some kind you can start off with say Anonymous login if you just want to get started without thinking about you know setting up the email addresses and passwords um and then you enable flexible sync which will basically set up a the synchronization service for you um and then you can enable that in developer mode so that when you set up your classes in your application and start writing and using it automatically um it will do the work of setting up the schemas that talk to mongodb so it sounds pretty complicated uh it does yeah once you get started with it it's it's great um we're trying to make that even easier
every day um but that is definitely like one of the the toughest parts to to get started but it's also like from my podcast experience it's quite hard to explain technical topics without visuals without like examples of like here is the dashboard and here is this piece of code that I'm like it's harder to describe the piece of code than just show it and like everyone would understand it when when they see it so right like you said we're gonna link everything that we are talking about in the show notes so that everyone can and golden and just set up their first chat application with with realm right uh yeah peace oh yeah I was just going to mention like we we can add also in the uh the notes like I did a session with Yemen Holmgren from uh react native live where we uh basically did a live stream on Twitch setting up a react uh Native app with realm and sync so like the first half of it we just set up the local to-do list app and then the second half of it we we did the entire uh Atlas uh cluster set up and get everything worked on and ran into issues and solved the issues uh so it was it was good um but at the end of it we got it working so if you want to check that out and see how that could be done um that's definitely worth checking into we're gonna link a show from Daemon as well in the show now the J1 is actually uh from Infinity dread they just had a chain react conference and they are doing react native radio which I'm a fan of and they he also does the live session with coding so I really uh I really enjoy watching him doing all of that um I want to move on to like maybe discuss some of the distinctive features that realm provides to developers and to like Up Performance and and all of that if you could speak something to this
right so um we've got all these points but I I want to talk about like how as I said each SDK that that realm has is is developer Focus like we're thinking about how how can you make it so that the database which is you know usually a super complex thing like you don't want the developers to have to think about a database yeah you want you want things to just you want to focus on your your application that you're making you want to get your ID off the ground so um the way we do that as we talked about before like having objects in uh that you can access around that that are basically uh the same as their JavaScript counterpart for example like the collection you get back from Realm looks and feels like an array right you can plug it into the data property of a flat list uh you can index it you can call map filter you can run 4-H methods uh iterable methods as well um the next feature is uh would be like live objects like the data that you get is always uh it's never stale it's always the um like every right you make you're always having the access to the version uh the the correct data um what's also nice about realm is the hydration period like if you if you're using something like Redux persist or or um state tree um you'll have to basically have a hydration period right you're gonna have to uh what does a hydration period mean basically when you start your app you're gonna have to load that state somehow into memory and um usually the way that's done is that you'll use something like async storage or or persistence Library under the hood which is going to
have to parse that data that was stored and convert it into a JavaScript object and then at that moment of time it sends a uh an event list uh it fires an event listener app to let you know that the hydration is done and your app can carry on and use that use that data now uh with realm um if you have access to your local if you have that local database you basically can open it directly right away and immediately have access to your data we don't have to load everything into memory at once we can use memory mapping features to make sure that you just get access right away and as you start accessing your data it'll start loading things into memory right so if you have a collection of you know hundreds of thousands of items you're not going to put hundreds of thousands of items in your memory right away you know you're going to get it yeah so it's faster right exactly okay all right um and then other than that uh we're talking about sync um you also get atlas uh you get access to the mongodb atlas features as well on top of that so um basically outside of the the realm database object that you have there's also an app object which would provide providers for and also a user object which is your authentication and from there you're able to basically call things like function Triggers on your Atlas State Atlas database so if you need to do any like mongodb specific things and you know quite well it um you can actually call function triggers and and pass them params and that you know you'll have functions in it's like serverless functions within the the mongodb cloud that can be triggered to actually
work on data right no okay that's interesting yeah right and at the end of the day as well like even if you did uh set up realm with sync and you wanted to do something besides like react native development you can also like create a graphql interface like mongodb offers the graphql interface to the data it can also generate a rest database to your data um right and uh yeah um maybe uh maybe what we could do now is also um take a look at the future for realm what are you cooking for us uh that is not released yes if you can speak something to that right uh one thing uh one of our teams is working really hard on is uh wasm support so uh that we could run realm actually in your browser so um I would love this because at that like after I was at the last conferences and seeing all the fun things that Expo is doing with like routing and and stuff like that file based routing I was like it you really want to share all your code you know between your your react native app and your react front-end app and if we can get the wasm support uh released and it has the same you know npm install realm and MP install realm react uh you would be able to share code between your web application and your realm application yeah um we do offer uh realm web today but it's basically um giving you a mongodb client that you can talk to and that's like the authentication things and stuff like that but you can't actually like work with realm in a classic sense in the browser today so that's super exciting uh we actually have Alphas out for that now there's actually people experimenting with it so um you can definitely check that out um
yesterday I just pulled the trigger on a release candidate for version 12.
um we're pretty excited about this because uh realm classically like realm.
js classically was mostly written in C plus plus so you would have basically methods that you were invoking uh that would just immediately be C plus plus calls um and for us that was a bit tough to maintain when we wanted to make new features so basically now we've created this binding generator that'll just wrap realm core and give us a typescript interface to it so now we can actually use like like our team can access the the realm core itself as a as a typescript interface and then write all the the SDK code and typescript and JavaScript I think this is going to be really cool because now people using our code are going to be able to step into realm and actually see what's going on under the hood and maybe submit some pull requests or you know file some bug reports with actually this context because classically it was difficult to see what happens once it goes into C plus plus land right yeah yeah yeah and uh we've done some performance checks as well and like we're not taking too much performance hit doing this so we're really excited about it so that's been uh that's been released um basically if you update from 11 to 12 you shouldn't have to make any changes to your code like we did the best we could we had to hold back for making breaking changes but uh yeah version 13 will be the fun one when we actually get to improve on that and then uh also um realm react itself um I just released a new version uh 0.
5 um this includes hooks to make it easier to authorize your app so you got to use out hook um that you can use uh basically if
um we're pretty excited about this because uh realm classically like realm.
js classically was mostly written in C plus plus so you would have basically methods that you were invoking uh that would just immediately be C plus plus calls um and for us that was a bit tough to maintain when we wanted to make new features so basically now we've created this binding generator that'll just wrap realm core and give us a typescript interface to it so now we can actually use like like our team can access the the realm core itself as a as a typescript interface and then write all the the SDK code and typescript and JavaScript I think this is going to be really cool because now people using our code are going to be able to step into realm and actually see what's going on under the hood and maybe submit some pull requests or you know file some bug reports with actually this context because classically it was difficult to see what happens once it goes into C plus plus land right yeah yeah yeah and uh we've done some performance checks as well and like we're not taking too much performance hit doing this so we're really excited about it so that's been uh that's been released um basically if you update from 11 to 12 you shouldn't have to make any changes to your code like we did the best we could we had to hold back for making breaking changes but uh yeah version 13 will be the fun one when we actually get to improve on that and then uh also um realm react itself um I just released a new version uh 0.
5 um this includes hooks to make it easier to authorize your app so you got to use out hook um that you can use uh basically if
you're setting up your Rail map for for sync um you have a user provider that has a fallback property when you when you don't have an authentication session and then it'll render what's in that fallback um so I made these hooks that when you're in that fallback you can just use these authentication hooks to have a simple interface for instance you call login with email and password which you pull from use off and it also has a pending or no it has a result object that shows you the state of the operation you made so we can all log in then it'll say okay this is pending or there's an error or it was successful and then you can dry with that it's like right yeah so uh yeah yeah exactly so this is like an async operation um but it's wrapping uh the authentication parts of Realm um and then use Query has been updated as I said to use these uh the callback with the uh um to set up your queries and your your filtering um and sorting and yeah [Music] um we're constantly Gonna Keep Looking for Community feedback um so uh realm react is a work in progress like I was really excited to make it and actually make it so that realm was easy to use so um we're definitely looking forward to to more Community involvement and seeing what people think about it and we welcome anybody to to submit issues or feature requests and um let us know what we can do we're listening we're very active on GitHub so and on podcasts and on react native live as well yeah I I really appreciate you coming into the show and uh talking with me about this I'm not really into databases so that was a very nice
opportunity for me to like learn more about the differences between the different kinds and like what I can do with them and I'm gonna definitely check out the synchronization because I feel like from my newbie database perspective this can allow me to like omit using the API calls entirely so I'm gonna I'm gonna I'm gonna check that um Andrew thank you so much for uh coming into the show I thank you everyone that was listening um we will link everything that we discussed uh in this episode in the show notes like uh realm react like mongodb react native uh and like react native life with Jamon that lasted three hours uh yeah thank you so much yeah thank you it's been it's been a pleasure [Music]
Timestamps
Show
Guests

Andrew Meyer
Accomplished Engineer
@
MongoDB
stop that react native EU is back for an on-site addition write that down September 7th and 8th in vrodsworth you can expect a industry thought leaders insightful talks networking with great deaths from all around the world and of course Good Vibes only write that down September 7th and 8th but also have get your tickets now do it hello everyone and welcome to the react native Show podcast uh today we're gonna talk about databases in mobile applications so to do that I invited Andrew Meyer who is a ex stuntman and software engineer from mongodb and we'll be talking about Rel so uh Andrew hello welcome to the show can you please introduce yourself yeah hello uh I'm Andrew Meyer as we've heard uh I'm a senior software engineer at mongodb I've been there for about two and a half years um I was hired on the realm.
js team uh to basically do uh the react native integration so classically realm.
js was developed for node applications um it's kind of like a server language and uh people are having troubles using it in react native so uh I've been helping with the uh the react native integration and yeah awesome I classically come from a react native background um before before I was hired I was a react native developer since about 2017 so uh yeah somewhere around react native version 35 or something like that oh yeah okay so that that's quite a long time ago the 35.
was part of the whole equation yeah yeah yeah can you can you please maybe explain to me as a newbie uh in like the databases environment what's the difference between like mongodb and Realm well mongodb is uh is a server database like uh application so you're basically running a server uh basically running
js team uh to basically do uh the react native integration so classically realm.
js was developed for node applications um it's kind of like a server language and uh people are having troubles using it in react native so uh I've been helping with the uh the react native integration and yeah awesome I classically come from a react native background um before before I was hired I was a react native developer since about 2017 so uh yeah somewhere around react native version 35 or something like that oh yeah okay so that that's quite a long time ago the 35.
was part of the whole equation yeah yeah yeah can you can you please maybe explain to me as a newbie uh in like the databases environment what's the difference between like mongodb and Realm well mongodb is uh is a server database like uh application so you're basically running a server uh basically running
the service on the server that's that's listening to multiple connections it's uh we're handling a lot of uh concurrency and realm is actually a local database on your phone so it's uh basically only just handling that single user at one time and um also between the two differences uh how how you access the data like in in mongodb you use queries uh and and you know get some Json back whereas realm is integrated within your language it's an object store so you basically get objects back you get they look like JavaScript objects but they're actually realm objects because you if you access them it's actually running C plus code that that we hand crafted so it's uh super performant super fast and uh you don't really feel like you're working with a database it just feels like you're working with Native data structures so it's kind of a difference or Mobility B it's yeah I guess we'll be talking about like Realm functionalities and certain around technology in the later parts of the episode so what I want to start this episode strum is to briefly go over like the data in Mobile in general so not only databases but also like data structures and the way we handle persistence data so can you tell us something about the history of uh databases in Mobile before we had Android and before we had uh iOS there was Nokia right right and yeah Nokia uh had their own um I think it's called like Series 30 and uh phones and they they didn't really have a database because we didn't have applications back then so they weren't you basically just have these like little little apps you would run right there'd be like you know storage of contacts and stuff like that but it wouldn't have like a classic database
the way you would think about it today I wasn't until their their later models that they actually started having like SQL light involved so if you're just gonna run you know snake on your phone you don't really need a database right but it wasn't until the uh the application space started where we could really we really needed some more data intensive applications and sqlite was already uh uh developed and already in usage in Nokia forms uh it wasn't it didn't get there for a while actually like sqlite was originally developed as an embedded uh system database for battleships actually I think for like missile targeting systems and so yeah there wasn't really anything uh available at the time that was just running locally on a uh on a single uh chip that that someone could access most databases ran in mainframes so um it was uh originally developed so that they could run it you know basically on a single application um and then it was open sourced um and picked up by the Android team uh as basically the database that that Android applications are going to use and then started becoming the the mobile uh database that exists on Android phones so it was pre-baked into the phone itself iOS devices as well by now this is this is also my knowledge that uh iOS devices and Android devices have that sqlite integration already there already uh yeah available and iOS also had another database that was called core data uh this was actually a super Antiquated uh system out of the next OS era like way way back when um you know next is uh the X and next is where the X and X code comes from so that's like how far you're going back oh really okay free Macintosh right so uh yeah uh core data I guess from what I've
heard I've never used it but I've heard that it was extremely difficult to uh to work with like if you got it wrong it would just crash and um I think we have Objective C from the same reason that it will it used to be in next operating system and then it got it got picked up by IOS as well right right yeah we're just like Mac Mac apps in general like Macintosh apps in general so like that whole yeah so what is the story uh going from like this embedded database sqlite that you have already on Android and for in an iPhone what was the reason that realm was created then so uh I was talked to the founder Alexander styxon and uh or sticks and sorry he uh actually was working at Nokia and he left the company and wanted to make a uh his own his own company where he's developing a code editor uh that was collaborative kind of like Google Docs now and so he was trying to figure out a way to do that uh using the tools like sqlite and things like that because even if you're making a desktop application it's still a local database doesn't have to be on a phone so uh but he was having like you know thousands of crazy relations as you're dealing with every single change that occurs and all these change sets everybody's finding that SQL light wasn't performing and so we decided to uh boldly just try and develop his own database um and then he realized like this could actually be the company that uh that uh he he could be starting and so he was working on that and decided in in 2011 to just try apply for y combinator and uh they got accepted and uh they moved to San Francisco it was they were originally in Copenhagen and basically started realm database um and I think they got picked up like started gaining popularity around the time Swift was released
they had originally uh like the the big projects that I worked on why combinator was like an objective-c version of Realm and uh as soon as they were about to to release uh the WWDC just like announced that they're releasing this new Swift language so they had to quickly uh panic and adapt and so that yeah so yeah and one interesting bit of information there is that at the same year that realm was in the Y combinator another very popular solution was in the same batch as well right right exactly so uh Firebase was also an Alum that year so uh Alexander was working with the the owners of Firebase at the same time collaborating on databases in the in the mobile space so yeah yeah and they both kind of branched off in their own different ways as we can see yeah yeah we will make some comparison to Firebase and uh realm in the uh in the end of this episode so uh going back to the like initial question of why do I never even need a database uh I'm a react native developer and like I'm completely satisfied with the options that I have the options that are really easy and I don't have to um add any additional package to get so what I have right now is essing storage which sorry it's not true I do have to add that package it's no longer in the car but like this is very like Plug and Play Right async Storage uh which you could call a very very simple database according to right like async storage is a key value store so it's a very simple database uh it's at the end of the day it's it's just a file right so you need to make a key it's it's saving uh a string and that's it you have different keys for different strings uh in the most case the string is a parsed Json object that you have to stringify to save and parse
to get back into your application so for simple applications that's that's really good but if your local data is significant uh it could take some time to uh to load that and hydrate it and also to keep track of it right to track the state on it yeah so like going on with your uh like growing example if I have a growing data structure then I opt for Redux or mobex or other State Management libraries and I can uh say persist my data and then hydrate it and I guess under the hood they still use the async storage but then I have some more fine great control over like the shape of my data structures right and that's a that's a valid option I mean it's it's it's legitimate uh but at the end of the day like you have everything in memory at that moment in time right you don't have any and you're constantly just just reading uh your object you're updating your state it's uh you're persisting it like you have you have that whole uh stale data that you can look back on which is kind of nice after a while but if you don't need it you don't need it right you're not cleaning up as often so you're building this this giant state tree and this giant uh memory space when you're using uh the State Management systems and classically that's good like for web that makes sense right you in web you don't have a local database you're getting information usually from a rest API and you need to just store it in the memory of that browser and work with it imagine that state but when you're on an actual like mobile device you can actually you can leverage the hardware that's that's in your hand right and you could actually have a database that stores all this information and accesses in a way that is super performant is you know putting it in memory the way it's supposed to be and not just relying on
the JavaScript runtime itself to to handle the memory um which yeah yeah you do any Research into that you kind of know that JavaScript isn't really super duper efficient when it comes to memory storage um right yeah and what would be so I guess at the end of this ladder we would go we will talk about realm what would be the step between State Management libraries and uh persisting and hydrating those and realm uh I guess the SQL light kind of things what what are those right so you could leverage uh sqlite that's on the phone right now using I mean you could you could just leverage it and write these uh SQL strings yourself and you know but typically people don't want to do that they don't want to write all these SQL statements all day it feels like you know a class you took in college right yeah yeah so um they're uh there are a lot of orms out there so object relational mappings and basically that's uh kind of an object syntax on top of the database so as your your accessing this object that's that's uh thank you kind of modeling the data that you you have on your phone it's automatically under the hood making these SQL queries for you and you know update queries select statements filters and all that under the hood and um that's fine but it's it's kind of a pain like if you for example have like a collection of data with some sub collections then you have to you know create a join table or if you want to have a many many relationship like you have to basically have another table in your database that's just IDs that link things together and if you say have get a a person out of the database and the person has a collection of dogs when you dot dogs
it's they don't exist yet it has to go in query now it has to go to a join under the hood and then populate that for you right so there's a lot of these these queries that are happening um and if you want to do anything uh more complex maybe at the end of the day you still end up having to write these queries yourself like from for the most part or I'm just handle about 95 of what you want to do but if you know that five percent is when you actually have to get under the hood and learn how that database works yeah I wonder also about like uh type safety in this kind of applications and like Dev experience the autocomplete stuff like that how how is that how does that work uh basically you'd have to model that yourself right you have to you have to write your own Types on top of that and hopefully expect that that's what's going to come back from SQL itself um the types aren't generally like you know one-to-one mappings from typescript two to what you have in sqlite um rum also has its own types that don't completely map but uh We've we've created like a class-based model um uh Syntax for Realms so you can model your data in a class uh which directly maps to what realm will be um it's actually we came out with a babo plug-in that kind of reads the class and actually like generates the schema for you it's a it's a bit of a experiment at the moment I'm kind of thinking more in the way of like going in a code generation route where like you define your models and I don't know if you've used graphql before like you know graphql code gen where you can write your graphical queries and then just runs over your code base and then
you get these nice like uh these nice convenience functions you can use or convenience types that are just small enough yeah I have I think that's something we're going to look into the future at the moment you define your things in class you get them typed and what comes out of your database is is typed right when you say give me all the round dot objects if a person you pass that person class in there you get a person back that's typed you get the autocomplete you can see what types it has um at least for most of the orms I've seen uh you basically get your objects with a string saying like the uh the collection I want is called persons and that's a string and then you have to use the generic type that you self-defined to to get that out of there oh okay yeah right so if that all RMS are s-colides so relational databases are under the hood uh how is the realm different how it differs from relational database SQL database right so realm is an object database and uh which basically means you get these objects back we talked about that a little bit earlier yeah but it's also it's not an interface on top of you know an existing database like you know you're at you're accessing realm directly um classically all the the teams that we have in our organization are based on the platform and languages that people develop apps in right so we have a swift team a Java team kotlin team flutter team and they are developing basically a layer on top of the core they're writing in C plus plus so that you get a version of the database that looks exactly as it should in the language that you're using and uses the same paradigms and then you are doing the bridge on top of that and giving us the react native right exactly
so we had a JS version that was exactly you know feels like JS and then uh I created a realm react Library that's basically providing react Hooks and providers so that you can access that data from realm.
js right so with all that said what is the actual benefit I get from this being uh object database the benefit you get is uh ease of use like it's a nice developer experience you don't really have to think about how your data is being stored in an efficient way you can just access it directly and also you get speed out of it as well all right so you you get instead of just having these JavaScript objects that you're working with all the time like you you get memory optimized versions of these objects and we can go in a bit more detail about how that's done um yeah please so like on High level I can see the difference I can see the analogy to to like old architectural react native and new architecture of react native so all architecture is a bridge that needs to send the messages as a Json it needs to be um Parts decoded stuff like that right and the native player then has to like act on those uh things so that's like or or Rems right then I have the SQL statements that needs to be understood and blah blah blah right in your like in this example the realm is new architecture when I have just direct access to C plus plus objects that are my state that are my data right and you also get the benefit that the data that you get isn't a copy of your data it's actually the live data so you're actually seeing the live results um and the way we manage that so that you don't run into issues is that if
js right so with all that said what is the actual benefit I get from this being uh object database the benefit you get is uh ease of use like it's a nice developer experience you don't really have to think about how your data is being stored in an efficient way you can just access it directly and also you get speed out of it as well all right so you you get instead of just having these JavaScript objects that you're working with all the time like you you get memory optimized versions of these objects and we can go in a bit more detail about how that's done um yeah please so like on High level I can see the difference I can see the analogy to to like old architectural react native and new architecture of react native so all architecture is a bridge that needs to send the messages as a Json it needs to be um Parts decoded stuff like that right and the native player then has to like act on those uh things so that's like or or Rems right then I have the SQL statements that needs to be understood and blah blah blah right in your like in this example the realm is new architecture when I have just direct access to C plus plus objects that are my state that are my data right and you also get the benefit that the data that you get isn't a copy of your data it's actually the live data so you're actually seeing the live results um and the way we manage that so that you don't run into issues is that if
you're updating that data we uh we have these write transactions so basically you define a transaction realm.
write you give it a call back and when you're inside of that transaction you're able to modify that data directly and um if you have multiple transactions happening at once uh realm core underneath knows like you know one transaction after the other if you do something that you're not supposed to then it's going to roll that transaction back right and then it's going to invalidate it and your data is going to be fine all right okay so that's something a bit different like normally you know if you're using like a state management and you modify the data directly like well it's only modified for that moment in time but it's not actually saved right if you tried to modify anything that that you get from Redux right um but you would have to go that extra layer you know sending a a request to your state tree to update the data itself but with realm you can just update it directly within there and I guess the next question is how do you get updates then like how do you know that your data updated um we also have event listeners that you can apply to um but you don't have to when you're using realm with react native with the hooks that I've developed I've actually created the event listeners for you which will return an update it'll basically trigger a re-render whenever any of the data updated if it's relevant right okay yeah maybe we can actually like start discussing this part um so and we will go back to the to the nice things and user experience and and all of that as well later but maybe while we are in event listeners can you
write you give it a call back and when you're inside of that transaction you're able to modify that data directly and um if you have multiple transactions happening at once uh realm core underneath knows like you know one transaction after the other if you do something that you're not supposed to then it's going to roll that transaction back right and then it's going to invalidate it and your data is going to be fine all right okay so that's something a bit different like normally you know if you're using like a state management and you modify the data directly like well it's only modified for that moment in time but it's not actually saved right if you tried to modify anything that that you get from Redux right um but you would have to go that extra layer you know sending a a request to your state tree to update the data itself but with realm you can just update it directly within there and I guess the next question is how do you get updates then like how do you know that your data updated um we also have event listeners that you can apply to um but you don't have to when you're using realm with react native with the hooks that I've developed I've actually created the event listeners for you which will return an update it'll basically trigger a re-render whenever any of the data updated if it's relevant right okay yeah maybe we can actually like start discussing this part um so and we will go back to the to the nice things and user experience and and all of that as well later but maybe while we are in event listeners can you
please um try to explain to how how do I start using this how is it built like on react only site what are the paradigms that you are using you mean like uh what you would do to get set up with with realm today like yeah yeah right so like I mean npm install realm and uh at realm slash react those are the two libraries realm itself being the realm.
js SDK and then at realm react being the uh react native layer on top uh and then you would think about how you want to uh what data models do you want to save so you would write your your classes right or your data models basically defining a schema of what you want to do and then um you take the you input the ROM provider in your application to wrap your app and you provide that with um schema array array of schemas right and these schemas are actually that you defined yeah schemas are just the JS classes uh yes there's actually uh if we get down into it there's actually two different ways you can model data like before typescript was huge in classes were kind of big in JavaScript um we just use schemas schema objects themselves so basically it's just an object it has an uh a structure that you have to um adhere to which is a name a name field which is the name of your model and then a properties field which shows you which properties are going to be in that um model right and each property can be like name which is the type of string age which is a type of integer um birthday which is a type of date and then you can even go further like if you you can set up like object links so if you have uh children for a person then you can say okay well I can just
js SDK and then at realm react being the uh react native layer on top uh and then you would think about how you want to uh what data models do you want to save so you would write your your classes right or your data models basically defining a schema of what you want to do and then um you take the you input the ROM provider in your application to wrap your app and you provide that with um schema array array of schemas right and these schemas are actually that you defined yeah schemas are just the JS classes uh yes there's actually uh if we get down into it there's actually two different ways you can model data like before typescript was huge in classes were kind of big in JavaScript um we just use schemas schema objects themselves so basically it's just an object it has an uh a structure that you have to um adhere to which is a name a name field which is the name of your model and then a properties field which shows you which properties are going to be in that um model right and each property can be like name which is the type of string age which is a type of integer um birthday which is a type of date and then you can even go further like if you you can set up like object links so if you have uh children for a person then you can say okay well I can just
make another person that it links to and then so you can even self-reference uh the same model within the model okay or dogs you can have a dog object and have that also represented that class so and then after you have those schemas defined then uh you you wrap your app at the realm provider and when you want to use that data within your app then you import use Query out of Realm uh you pass that in that class that you want to get out of it and then it will return the collection of uh of data okay all right and then yeah yeah please continue right uh and if you want to filter that um since the last version I just released uh usually you would just call uh dot filter down that and wrap it and use memo or something like that but uh I created a second argument to use Query which is a callback where uh you get a access to that collection that you um that you want and then you can call Dot filtered within that callback and then you get a memoized uh collection back so it's just gonna uh you can even pass into dependency array if you want to like sorry let me go back to dot filtered um dot filter has like a query uh syntax like age greater than five you know give me all the persons with age greater than five so you put that in there and now you're gonna have a collection that's always going to have all the people that with their age greater than five right no okay okay and then I was before you said that I was going to say oh okay so this is almost like selectors in Redux so that I get my data and then I can speak something else from it as well but then with the introduction of this like uh conditions right you can like actually select data more more precisely
exactly and uh what's kind of nice is you can call sorted as well uh which can Define how you sort that data and imagine like you're displaying a list of people and your UI has a way to filter and sort so if you have a use State up above that's basically keeping track of what you selected for filtered values then you can use that state as a dependency array into use Query so that it will rerun that uh that query when those objects update say that age is a is a scroll uh and you need to select which age you want like and you move it to 10 then it's going to re-update that query and say okay we actually want people that are greater than h10 and we want to sort it alphabetically from you know descending yeah and yeah that could change as well and um that happens really fast it's like it doesn't need to make an extra you know SQL query when it does that it's just modifying that that internal query and you'll get the results back um so I want to touch on something that we discussed uh with you and some other people from your company from Realm I I could say Monaco to be your company but yeah uh that I've heard this nice phrase that mobile is uh ultimate distributed database and on top of that you have just released um this new feature of synchronization between uh yeah maybe maybe you can say that what's the feature is and how it connects with this like distributed database right so since realm's uh marriage to mongodb we've been uh we have access to all their their Enterprise uh uh or their uh server offerings right um the atlas app Services uh and with that you're able to have a global database that actually can communicate with that local database so each each of these applications that are out uh are basically having a copy of that that
um that Global database that mongodb offers and synchronizes with it right so now you have a basically a distributed system of uh many different mobile devices out in the field that have various network connectivity issues or maybe they're offline they're able to access their data they're coming back online now they need to say what's changed what's different and [Music] um basically the uh the server will handle that for you and actually be able to send change sets from from other pieces of that data and be able to come back and have the exact um reality of what the world is in that moment right so um moving on to that so that's basically what we like to call an offline first uh architecture so um your data when you have a local database you'll always be able to access your data when you're offline you know if you're very first time open up the app um you have nothing right so you have to download the the data that you need at that moment in time um and we have a a trip for that it's called flexible sync basically you subscribe to a uh a subset of that data that your app needs typically if you in most apps it would be just your user ID is probably what you want to filter your data on when you authorize right and then you you can start you know working on your data and after that's done then you basically have this offline first app you can edit your data at any time so Andrew from what I hear about the synchronization this offline first part is that I could theoretically have my database uh be a substitute to my like API calls
to have only data like being populated through database and not through uh calling the API right so like I like to think about it so In classical you know uh architectures uh you know for like web applications or any other mobile applications right you usually making rest API calls to an API and caching it caching the result if you need to update things then you have to make a request a post request to update that data and then you have to do another request to get the data back you also have to set up a probably some sort of subscription like a websocket or something or some sort of polling to to keep getting the most updated version of that data um but when you're using realm with sync it's basically like your database becomes your rest API right so as long as you're setting up a listener to that collection of data you're always getting as long as you have a network activity you're always getting the most updated thing you know it's already subscribed to to changes and you get updates when those things change and if you write to it it's automatically going to update what's in the cloud so yeah the like the challenging example that I can think of when it's quite hard to synchronize is chat applications right because like you need it to be like Fast reliable all of that and like on time and like I feel if someone takes the burden from me of doing the hard work of synchronization and I can just focus on like creating the UI and it's gonna work like this could be a very good use case to like not do the API completely and just rely on the database underneath right exactly so we just take all the hard work out of it yeah you could basically use something
like flexible sync to apply to a chat room and then you know then you just get the subset of data from that chat room so you could easily have multiple chat rooms running against your your mongodb cluster um but your mobile app that's that's currently subscribed to that chat room will only see the changes it's interested in and that should happen quite fast yeah so I think that in the beginning of the episode that we will compare the Firebase and realm so let's do that now let's maybe um have that comparison how is what I can do with realm and what I can do with Firebase what are the differences there in possibilities they're definitely very similar in uh terms of what you can do right uh Firebase off let's talk about firestore I'll talk real quick there's two things that Firebase offers they have the real-time database solution and the firestore solution uh I would recommend if you're going to use Firebase look at the firestore solution well the realtame database is uh it's basically just storing Json at the end of the day right it's just an object and firestore you can you know set up Collections and sub collections um but what is the difference between Firebase and realm they both offer synchronization they both offer queries you get your updates but Firebase is uh when it comes to local right um I would say Firebase is more remote first whereas realm is more local first right um you are able to work on your data in Firebase when you're offline it will store all the transactions you're you're working on as you're as you're making updates to it but it I was just reading through the manual at some point like if you if
you're setting up a really complex transaction uh and you restart your app you will lose that those changes that you just made so um and realm it's a bit different like you we're also like you're making all these changes to your database we're also keeping a log a change log that's going on but we're not going to wipe that out for you like your local database will will be be the same there are situations of course where you can get in this Stitch uh the situation that like this for instance if your app's been offline for a very long time and the schema has changed on your on your mongodb data like the structure has changed there's like no way you're going to be able to uh to migrate um then basically at that point you can just wipe the data on the phone and get the uh the latest changes downloaded right something that I picked up from our conversation previously is that it works similarly to uh git right uh when yeah yeah when you have conflicts you have to resolve them sometimes it is not like you said just now sometimes it's not possible to resolve the conflicts but you can have your everyone has this distributed version of called um you can make your changes locally and eventually after you push resolve conflicts it will be what's the word consistent on eventual consistency it's called right yeah so that yeah so that as you're working on that copy of your data and git at some point you're going to want to you know merge those with the changes that are up there and you do that in the local you know you could obviously just say from your client Force push and override all the changes your your teammates made but I'm sure they they're not going to like you after that
sure so yeah it's very similar the same type of problems uh that you have would have to solve with merge conflicts is kind of what we're solving for you with uh with realm data and uh in a cloud database okay so maybe talk let's talk a little bit about the use cases we touched on the very simple solution like icing storage um and like statement libraries but then what is the the the this step what are the use cases that you would Advocate to use more advanced solution like like Realm I think any even simple apps like could benefit from using realm as you know if you ever need to do anything especially with you know realm and sync if you ever need to do anything with a back end and you're setting up you know an API and a back-end server and a database thinking about distribution and things like um it's nice to just use realm right away uh because then you don't have to like think about these optimization things right you know it's not hard to get started with realm and you know like there's a classic like you know don't pre-optimize your code because you'll never get to release well if you just start using realm and sync right away like you're basically pre-optimizing without wasting any time so that's definitely uh well if we'll move on to the next Point like what are what are specifically good applications for realm um if you're in areas with low latency um say if you're creating a logistics app and you have uh truck drivers uh using a mobile app to know like where they have to you know deliver their packages to um you don't want them you know being out in the middle of the country trying to
see okay where's that address and all of a sudden they get a loading spinner or at the same point they want to report right away that they uh their job is done and that they've delivered their package and um if they can do that with Rel they can simply modify that data and as soon as their mobile device gets Network again it's going to send and update that change set so that the people in the logistics Center can actually see oh okay they're done with their delivery right um it's fun we actually even had a a GitHub user um filing uh issues for us uh from uh who's actually a farmer in Africa and he's like making apps for farming you know Africa typically doesn't have very good low latency and so he's able to actually do some tracking of his uh his farm farming operation using realm and sync so okay so and so basically uh it's from what I from what I'm hearing is that it's good for everything but especially good for like um Locker first database which it is uh for low Network conditions stuff like that right yeah it's also nice like if you if you do want to have you know just the local part around like if you have an application that's like a note taking application or anything like that um it's nice to use realm because you your data stored in kind of more of a database instead of just a giant file like it'll it'll take up list space on your disk um yeah if you're right so I was going to also ask because we already discussed how to set it up in my project but how do I set up the synchronization do I need my own server with mongodb or do you have some kind of maybe pre-made solution that I can hook into uh there is a little bit of a setup uh
we're trying to make it easier and easier but you do we have a we have a free tier for sync like uh sync itself isn't isn't free but after you uh you scale to a certain point you do need to set up an atlas um cluster um so basically you can make an account on on mongodb and and set up an atlas and that you deploy cluster and then you create it basically it's under app Services you create a an application and that is what you can use to start sync we do have some easier ways to get started like if you um we're going to share some links uh uh the getting started tutorials um which will deploy basically like a to-do list app and automatically set up like your Atlas and app services for you and I'm wanting to like make a command line tool in the future where you can basically just say like react uh realm in it and then it will just set up that background for you um but yeah so let's take that back a little bit so once you get set up with app Services um you basically create a realm app you set up authorization of some kind you can start off with say Anonymous login if you just want to get started without thinking about you know setting up the email addresses and passwords um and then you enable flexible sync which will basically set up a the synchronization service for you um and then you can enable that in developer mode so that when you set up your classes in your application and start writing and using it automatically um it will do the work of setting up the schemas that talk to mongodb so it sounds pretty complicated uh it does yeah once you get started with it it's it's great um we're trying to make that even easier
every day um but that is definitely like one of the the toughest parts to to get started but it's also like from my podcast experience it's quite hard to explain technical topics without visuals without like examples of like here is the dashboard and here is this piece of code that I'm like it's harder to describe the piece of code than just show it and like everyone would understand it when when they see it so right like you said we're gonna link everything that we are talking about in the show notes so that everyone can and golden and just set up their first chat application with with realm right uh yeah peace oh yeah I was just going to mention like we we can add also in the uh the notes like I did a session with Yemen Holmgren from uh react native live where we uh basically did a live stream on Twitch setting up a react uh Native app with realm and sync so like the first half of it we just set up the local to-do list app and then the second half of it we we did the entire uh Atlas uh cluster set up and get everything worked on and ran into issues and solved the issues uh so it was it was good um but at the end of it we got it working so if you want to check that out and see how that could be done um that's definitely worth checking into we're gonna link a show from Daemon as well in the show now the J1 is actually uh from Infinity dread they just had a chain react conference and they are doing react native radio which I'm a fan of and they he also does the live session with coding so I really uh I really enjoy watching him doing all of that um I want to move on to like maybe discuss some of the distinctive features that realm provides to developers and to like Up Performance and and all of that if you could speak something to this
right so um we've got all these points but I I want to talk about like how as I said each SDK that that realm has is is developer Focus like we're thinking about how how can you make it so that the database which is you know usually a super complex thing like you don't want the developers to have to think about a database yeah you want you want things to just you want to focus on your your application that you're making you want to get your ID off the ground so um the way we do that as we talked about before like having objects in uh that you can access around that that are basically uh the same as their JavaScript counterpart for example like the collection you get back from Realm looks and feels like an array right you can plug it into the data property of a flat list uh you can index it you can call map filter you can run 4-H methods uh iterable methods as well um the next feature is uh would be like live objects like the data that you get is always uh it's never stale it's always the um like every right you make you're always having the access to the version uh the the correct data um what's also nice about realm is the hydration period like if you if you're using something like Redux persist or or um state tree um you'll have to basically have a hydration period right you're gonna have to uh what does a hydration period mean basically when you start your app you're gonna have to load that state somehow into memory and um usually the way that's done is that you'll use something like async storage or or persistence Library under the hood which is going to
have to parse that data that was stored and convert it into a JavaScript object and then at that moment of time it sends a uh an event list uh it fires an event listener app to let you know that the hydration is done and your app can carry on and use that use that data now uh with realm um if you have access to your local if you have that local database you basically can open it directly right away and immediately have access to your data we don't have to load everything into memory at once we can use memory mapping features to make sure that you just get access right away and as you start accessing your data it'll start loading things into memory right so if you have a collection of you know hundreds of thousands of items you're not going to put hundreds of thousands of items in your memory right away you know you're going to get it yeah so it's faster right exactly okay all right um and then other than that uh we're talking about sync um you also get atlas uh you get access to the mongodb atlas features as well on top of that so um basically outside of the the realm database object that you have there's also an app object which would provide providers for and also a user object which is your authentication and from there you're able to basically call things like function Triggers on your Atlas State Atlas database so if you need to do any like mongodb specific things and you know quite well it um you can actually call function triggers and and pass them params and that you know you'll have functions in it's like serverless functions within the the mongodb cloud that can be triggered to actually
work on data right no okay that's interesting yeah right and at the end of the day as well like even if you did uh set up realm with sync and you wanted to do something besides like react native development you can also like create a graphql interface like mongodb offers the graphql interface to the data it can also generate a rest database to your data um right and uh yeah um maybe uh maybe what we could do now is also um take a look at the future for realm what are you cooking for us uh that is not released yes if you can speak something to that right uh one thing uh one of our teams is working really hard on is uh wasm support so uh that we could run realm actually in your browser so um I would love this because at that like after I was at the last conferences and seeing all the fun things that Expo is doing with like routing and and stuff like that file based routing I was like it you really want to share all your code you know between your your react native app and your react front-end app and if we can get the wasm support uh released and it has the same you know npm install realm and MP install realm react uh you would be able to share code between your web application and your realm application yeah um we do offer uh realm web today but it's basically um giving you a mongodb client that you can talk to and that's like the authentication things and stuff like that but you can't actually like work with realm in a classic sense in the browser today so that's super exciting uh we actually have Alphas out for that now there's actually people experimenting with it so um you can definitely check that out um
yesterday I just pulled the trigger on a release candidate for version 12.
um we're pretty excited about this because uh realm classically like realm.
js classically was mostly written in C plus plus so you would have basically methods that you were invoking uh that would just immediately be C plus plus calls um and for us that was a bit tough to maintain when we wanted to make new features so basically now we've created this binding generator that'll just wrap realm core and give us a typescript interface to it so now we can actually use like like our team can access the the realm core itself as a as a typescript interface and then write all the the SDK code and typescript and JavaScript I think this is going to be really cool because now people using our code are going to be able to step into realm and actually see what's going on under the hood and maybe submit some pull requests or you know file some bug reports with actually this context because classically it was difficult to see what happens once it goes into C plus plus land right yeah yeah yeah and uh we've done some performance checks as well and like we're not taking too much performance hit doing this so we're really excited about it so that's been uh that's been released um basically if you update from 11 to 12 you shouldn't have to make any changes to your code like we did the best we could we had to hold back for making breaking changes but uh yeah version 13 will be the fun one when we actually get to improve on that and then uh also um realm react itself um I just released a new version uh 0.
5 um this includes hooks to make it easier to authorize your app so you got to use out hook um that you can use uh basically if
um we're pretty excited about this because uh realm classically like realm.
js classically was mostly written in C plus plus so you would have basically methods that you were invoking uh that would just immediately be C plus plus calls um and for us that was a bit tough to maintain when we wanted to make new features so basically now we've created this binding generator that'll just wrap realm core and give us a typescript interface to it so now we can actually use like like our team can access the the realm core itself as a as a typescript interface and then write all the the SDK code and typescript and JavaScript I think this is going to be really cool because now people using our code are going to be able to step into realm and actually see what's going on under the hood and maybe submit some pull requests or you know file some bug reports with actually this context because classically it was difficult to see what happens once it goes into C plus plus land right yeah yeah yeah and uh we've done some performance checks as well and like we're not taking too much performance hit doing this so we're really excited about it so that's been uh that's been released um basically if you update from 11 to 12 you shouldn't have to make any changes to your code like we did the best we could we had to hold back for making breaking changes but uh yeah version 13 will be the fun one when we actually get to improve on that and then uh also um realm react itself um I just released a new version uh 0.
5 um this includes hooks to make it easier to authorize your app so you got to use out hook um that you can use uh basically if
you're setting up your Rail map for for sync um you have a user provider that has a fallback property when you when you don't have an authentication session and then it'll render what's in that fallback um so I made these hooks that when you're in that fallback you can just use these authentication hooks to have a simple interface for instance you call login with email and password which you pull from use off and it also has a pending or no it has a result object that shows you the state of the operation you made so we can all log in then it'll say okay this is pending or there's an error or it was successful and then you can dry with that it's like right yeah so uh yeah yeah exactly so this is like an async operation um but it's wrapping uh the authentication parts of Realm um and then use Query has been updated as I said to use these uh the callback with the uh um to set up your queries and your your filtering um and sorting and yeah [Music] um we're constantly Gonna Keep Looking for Community feedback um so uh realm react is a work in progress like I was really excited to make it and actually make it so that realm was easy to use so um we're definitely looking forward to to more Community involvement and seeing what people think about it and we welcome anybody to to submit issues or feature requests and um let us know what we can do we're listening we're very active on GitHub so and on podcasts and on react native live as well yeah I I really appreciate you coming into the show and uh talking with me about this I'm not really into databases so that was a very nice
opportunity for me to like learn more about the differences between the different kinds and like what I can do with them and I'm gonna definitely check out the synchronization because I feel like from my newbie database perspective this can allow me to like omit using the API calls entirely so I'm gonna I'm gonna I'm gonna check that um Andrew thank you so much for uh coming into the show I thank you everyone that was listening um we will link everything that we discussed uh in this episode in the show notes like uh realm react like mongodb react native uh and like react native life with Jamon that lasted three hours uh yeah thank you so much yeah thank you it's been it's been a pleasure [Music]
Show Transcript
In this episode, we sit down with Andrew Meyer, an accomplished engineer at Realm/MongoDB with 10+ years of experience in software development to uncover the history, features, and unique advantages of Realm as used in the React Native applications
Realmand and the React Native ecosystem
- A brief history of mobile databases and the road from Nokia to Realm
- The overview of database options for mobile applications from the least to the most complex
- A deep–dive into Realm SDK for React Native, from benefits and use cases to features, usage details, and resources for developers
- Future plans for Realm
Resources for using Realm with React Native
- Building a to-do app with Realm React Native (Jamon Holmgren & Andrew Meyer)
- React Native Tutorial
- Realm React Native SDK Documentation
- React Native Template Realm
- Build a React Native Mobile App with Realm & MongoDB - in 1 hour: Flipper, Babel, Hermes, & more!
- Launch a React Native App that Automatically Syncs to the Cloud
- MongoDB Realm and React Native Getting Started
Integrating backend services with React Native?
We assist with seamless backend integrations for mobile apps.
Let’s chat
//
Insights
Learn more about
Backend
Here's everything we published recently on this topic.
//
Backend
We can help you move
it forward!
At Callstack, we work with companies big and small, pushing React Native everyday.
Web App Development
Build a solid foundation that meets latest standards and scales easily to other platforms in the future.
Next.js Development
Use Next.js to build fast, SEO-friendly web applications with server-side rendering at the edge.
