Rebuilding Bolt.Fun's Nostr-powered commenting system
So this past week, we've been hard at work on rebuilding our commenting feature according to our plans that we talked about in previous parts of this series.
We started with the back-end side of thing first.
As you may know, for each newly created story, we want to publish a root event on Nostr that will represent this story & the comments & replies will be able to later reference this event.
But publishing an event is not a 1-step process.
You need to connect to a bunch of relays first, create & sign the event, publish this event to all the connected relays, then finally close the connections.
This process usually takes some time, so it can't be executed on a serverless function.
We needed something different.
We first considered using Netlify's Background Functions.
These are very similar to the normal functions, however, they allow for up to 15 minutes of execution time before timing out, so they could solve our problem.
But there was a problem: Retry on Failure.
Publishing on Nostr could fail due to a variety of reasons. So just calling a background function one time may not be a very reliable way to handle things like publishing a Nostr event every time.
So Johns suggested that we use a queue for that.
After some researching, we went on with BullMQ.
We built a new service using Fastify.js & hosted it on Railway, & this service connects to a Redis instance & uses BullMQ.
So now each time we want to publish a new Nostr event, the serverless functions would call an api on the queues service, & then this service would add a new job to the queue that will be handled later by a specific worker.
I'm primarly a Front End dev, not a back end dev, so when it came to the system design, we got advice & help from Aaaljaz regarding that, so huge thanks to him for that.
So right now, we finished most of the needed work on the back end, & I deployed it to a preview branch on Netlify yesterday (I actually spent 3 hrs yesterday fixing weird deployment issues until it finally worked!)
But I'm actually very glad that we created this queue service.
Because aside from Nostr, several other stuff (like notifications for example) could benefit a lot from it instead of how we are doing it now.
Next week, we'll start working on the Front-End part of the feature.
I'm hoping that things would smoothly & we can get it tested & out soon.
& that's everything for what we've been doing this week!.
Until next time, have a nice day 👋