-
@ v.142857
2023-05-07 21:38:01Hyper-Nostr: Bringing P2P to relays
People suggested me to write a detailed post blog about my project, explaining how to use it, what it is, what to expect, and how it works. This post is the attempt of doing that.
What is it?
Hyper-Nostr is a decentralized nostr relay. It is a distributed relay, where everyone can have its own local node. Then, they behave as a single relay, sending and keeping the events on the network.
Right now, to use it you need to have
npm
, which requiresnodejs
. I may add plain executables on the future. You install it asnpm install -g hyper-nostr
and then you can start your own node ashyper-nostr [<port> [<topics>...]]
. Last step is to add the relay on your client asws://localhost:[port]/[topic]
for each topic you want to have on that client. I am still working on havingwss
instead ofws
, but looks like clients accept the connection because it is onlocalhost
. UPDATE: you can use some reverse proxy to havewss
. I personally use caddy so I can docaddy reverse-proxy --to localhost:3000
.Right, the topics. The topic is how we find each other, to send the events and have an unified database. Think of it as the address of the hyper-nostr relay, or a specific instance of it.
Having all configured, you can now expect it to work just as a standard relay: it will receive events that was send to these, you will be able to send and receive events, and it will save the events on it.
How it even works?
This project is Powered by Holepunch.
The connections and event streamings happen on Hyperswarm, Autobase linearized the underlying files, and uses a Hyperbeedee database (loosely based on MongoDB).
The hyperswarm and cores management was highly abstracted thanks to Hyper SDK.
Want to know more? Okay, lemme try to explain: - Hyper SDK creates a unified swarm instance that is used by the entirety of the relay; - Each topic creates two underlying hypercores: one that is the same for everyone in the topic, used to connect the peers, and the other is local, for your own local database; - Then, the discovery core has two extensions: the event broadcasting extension, to relay to the connected users the real-time events, and the syncing extension, to share the local databases to the peers; - Then, with the help of Autobase, all the local databases are linearized, giving an unified database shared across all peers. - With that, it is as if every peer has the same database when querying, and every peer receives the broadcasted events on real-time too (filtering by the subscriptions, as specified on NIP-01).
What to expect? (or, what are these logs)
The first message the relay prints is
your key is <key>
. This key is the hyperswarm public key, which for now I am doing nothing with it, but maybe in the future I will implement private relays that you connect specific peers on it. Still thinking on it.If you have passed initial topics, they will log here too. When you see
listening on <port>
, the relay is now open to receive websocket connections (aka it is ready for your client to connect).During the database indexing it generates a warning of
(node:<PID>) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 append listeners added to [LinearizedCoreSession]. Use emitter.setMaxListeners() to increase limit (Use
node --trace-warnings ...to show where the warning was created)
. This warning is expected, dont panic.To each websocket connection, the relay will also log 2 messages:
ws connection started
andws connection stablished
. They were first there to detect silent crashes during the topic initialization, that happens between these two logs if it didnt happen yet.To each topic initialized, it logs
swarm hyper-nostr-<topic> created with hyper!
. The prefix is to mitigate the risk of topic crashing on the hyperswarm.Finally, there is one last thing to expect: peers connections. It logs a count of peers on topics on each connection, in the format of
<count> peers on <topic>!
every time the connections update.What do you think? Do you liked this project?
Consider sharing your thoughts with me, I would love to know. Do you think the explanation was superficial? What could I have explained better? Do you have some suggestion for the project?
Consider supporting me, I have no formal job currently, and I am spending a lot of time and energy on this project.