How to build a Nostr relay

My ambition is to create a Nostr relay service running in cloud using modern Java technologies. :) Will be used to create private or paid relays using a friendly user interface. This is my journey!

How to build a Nostr relay
Do not index
Do not index

This is my journey of building a Nostr relays service in the cloud

So, first of all, there are a lot of relays on the market. Why do I want to create a new one?
  • I aim to create a new Java relay and a platform to create private/paid relays stored in the cloud (AWS).
  • I intend to create my own to experiment with the Nostr protocol end to end. I have already started implementing a Nostr client, nostrsky.com, and a relay is what I need to understand the Nostr protocol end to end.
  • Another reason is the fact that there are not so many Java implementations. People think that Java is old and not good for Nostr? But I believe that Java is keeping pace with the other languages.
  • And there is another personal reason. I want to keep pace with modern Java. All these are fresh in 2023: Spring 6, SpringBoot 3, and Java JDK 20, Jakarta EE 10.
My inspiration for the technologies I want to use is in this presentation:

The Nostr relays on the market.

Here are some statistics about the existing Nostr relays:
notion image
notion image
Here is a good description of the relay's relevance on the market:
Tips for relays \ stacker news ~bitcoin
“Tips for Relays. Forget about having a thousand and one relays. The first option is to pay for nostr.wine, it allows you to use the filter.nostr.wine filter. The filter works both requesting events from other relays and sending events to other relays. In case you want to send events to other relays you have to add the relay with the following syntax: wss://filter.nostr.wine/REPLACEWITHYOUR_NPUB?broadcast=true Reading list: wss://relay.damus.io wss://nos.lol wss://relay.current.fyi wss://brb.io wss://nostr.oxtr.dev wss://relay.nostr.bg wss://no.str.cr wss://nostr.mom wss://nostr.zebedee.cloud wss://relay.plebstr.com wss://offchain.pub Writing list: wss://relay.damus.io wss://nos.lol wss://relay.snort.social wss://nostr.oxtr.dev wss://relay.nostr.bg wss://nostr.fmt.wiz.biz wss://nostr.mom wss://nostr.zebedee.cloud wss://no.str.cr wss://relay.plebstr.com wss://offchain.pub The next special relay is relay.nostr.band. This relay reads events from all relays and applies a spam filter, in the case of writing you only write to relay.nostr.band but it is a good complement to add content to relay filter.nostr.wine as it only requests events from your contacts and your contacts' contacts. It is not clear to me from which relay is requesting the events as the code is not available. And finally the relay nostr.mutinywallet.com. This relay uses blastr which is a nostr cloudfare worker proxy that publishes to all known relays. Basically what it does is to read the list of online relays from nostr.watch and all events are queued to be executed in batches by another worker that rotates every 30s if there is an event queued, or once a certain amount of events are queued. In this case the relay is write-only. In short, your relays could be reduced to 4: wss://nostr.wine wss://filter.nostr.wine/REPLACEWITHYOUR_NPUB?broadcast=true wss://relay.nostr.band wss://nostr.mutinywallet.com With them you save bandwidth and battery, actually filter.nostr.wine in write mode and nostr.mutinywallet are redundant, but I put the two because nostr.mutinywallet.com has more amplitude because it publishes in all the online relays of nostr.watch and also because it has high availability in case of failure of any of the two relays mentioned above. The same case is applicable in the case of reading for filter.nostr.wine and relay.nostr.band.” Source: note1ukmaarf2t9dkhxkphwh4sv4qsrtnvxwz09wmfpgsujwh3m48h8gstyef2f [7 comments]
Tips for relays  \ stacker news ~bitcoin

What is a Nostr relay?

By definition, a relay stores Nostr events sent by clients and accept subscriptions from clients. Events are like this (in this case, we have an event of type 0 → meta-data):
notion image
It can filter and offer search for events: profiles, notes, etc. Also can offer anti-spam support.
The relays can be paid at https://relay.exchange or not. A list of worldwide relays is here: https://nostr.watch/relays/find
Relays browsers:

Starting the project

I decided to use SpringBoot 3 (Spring Framework 6) and Java JDK 20 to build the relays service. See this URL: https://spring.io
Some reasons:
  • support for Graal VM (native - improve startup time and lower usage of memory)
  • ready for virtual threads (Loom project - improvements of runtime speed)
There is a very convenient way to start a project with Sprin Boot, but I can’t use it now:
The lite version of JHipster is a good candidate because it already has the new Spring 6 / SpringBoot 3

Playing with existing Java libraries for Nostr

So, I search the entire github.com, and I get just one Java library for working with Nostr protocol, and I don’t like it very much. Github: I https://github.com/tcheeric/nostr-java
  • it uses lombock which has no future after Java introduced records (I used lombok in the past, but now it is obsolete)
  • too much inheritance in the domain model (this is a sin of the academic world - in real projects, the composition is preferred)
  • the way the project is structured, the maven sub-project is also not the best (it is better to be structured on NIPs)
  • in-house JSON encoder/decoder is needed?
I started to browse fiatjaf repositories on github.com. Maybe I can find something better:
If not, I will use this library and try to create a better one afterward.

Bootstrapping the project

I initiated the project using Spring Initializr with some libraries: web sockets, persistence, and others.
notion image
I initialized a similar project but with JHipster Lite.
notion image
I have to decide which of them to use to bootstrap my project!
A few days later….
I decided to use JHipster Lite because:
  • it has already Spring Boot inside 🦾
  • it has already UI (Angular) support
  • extra utilities
But, I will use Clean Architecture and not Hexagonal Architecture. They are similar as the final goal, but I like Uncle Bob's approach more.
And see here the trends:
notion image
I’m inspired by the following:
spring-shoestore
Github
spring-shoestore
Owner
spember
Updated
Aug 30, 2023

NEXT STEPS

  • integrate the relay with an existing client
  • publish the relay service in AWS
  • create support for external users to create relays (public and private)

Join other 213+ nostricians now!

Are you prepared to embark on the next significant phase of social networking?

Subscribe
Nostr Sky

Written by

Nostr Sky

Nostrsky.com editor