I recently came across an outstanding Open Source backend project called PocketBase and its really amazing.
Let's point out the good parts:
- It is written in Go, and everyone knows how _blazingly _ fast Go is.
- Open-source alternative to Firebase for smaller/hobby projects.
- Only one file to run, and can be used for RealtimeDB, Authentication(with different providers), File Storage, RestAPI and much more to come.
- Now the best part, it requires very very few lines to setup, and trust me its super easy to setup a realtime db really fast, compared to Firebase which has a lot of boilerplate and setup at client sideπ€―.
- Local admin dashboard to take control of everything, can be self hosted easily, only one single file to run everything. Isn't this Amazing! π²
Cons
Still under development so you might face some issues, but the development team is keen to resolve issues really fast.
- You can setup your own rules for the access to APIs easily:
I created this simple todo app using React+PocketBase, which supports creation and updation of todo in realtime.
- Open the Admin UI in browser and create a new collection named todoCollection and add these fields to it and for now disable list action,create action and update action API rules to grant everyone access.
That's it for the server side. Now coming to the client side:
- Create a new react-app with Tailwind if you want styling as mine else leave it and create a components folder with a file named TodoScreen.jsx.
- Install pocketbase js-SDK using
npm install pocketbase
and import it in your file using:import Pocketbase from 'pocketbase'
- Pocketbase doc is itself sufficient and also the Admin Dashboard as I mentioned above where you can get the codes for your APIs to use.
- Just an important code that deals with the realtime in React:
// realtime
useEffect(() => {
client.realtime.subscribe('todoCollection', function (e) {
// console.log('realtime', e.record);
let x = todos.filter((item) => item.id !== e.record.id);
setTodos([e.record, ...x]);
});
return () => {
client.realtime.unsubscribe(); // don't forget to unsubscribe
};
});
So what it does is, keep the looking for mutations in the collection as the client is subscribed to the collection. When some changes happens in the collection (create,update & delete) it gets fired and returns the new record.
P.S (not going to write full code here because its easily understandable)
Link to the full code: https://github.com/rajesh6161/pocketbaseTodo
Thanks, and hoping this was of some help for devs looking out for Realtime DB with easy and fast setup.
Top comments (1)
Nice stuff, but you might want to first do a fetch of posts when the app is loaded the subscribe to changes , because with that code it won't load any post initially and only do so after a user creates a post which isn't good UX