Nostr4j is a cross-platform Java library that handles most Nostr protocol details for you, with security, strong typing, and efficiency built into its design. Alongside hardening and careful memory management, it caches and reuses data to avoid unnecessary conversions and allocations.
The same code compiles for Linux, Windows, macOS, Android, iOS, and browsers through JavaScript or WebAssembly.
The newly released version 0.4.1 comes with a new website with the full documentation and live examples: https://ngengine.org/nostr4j/ , and with a lot of fixes and improvements, including:
Improved P2P networking
Nostr4j has a built-in p2p networking stack (aka NIP-DC). This is a special (and fairly complex) protocol designed for nostr4j, that lets users coordinate on Nostr relays and then move to direct and reliable p2p connections for encrypted data exchange.
Multi-hop routing and mesh networking
When two users can't connect directly, multi-hop routing lets their messages travel through others, forming a mesh network that users can join with at least a single connection to an existing member. Routing happens automatically, and onion encryption keeps private messages encrypted end to end, so forwarding peers can't read them.
Distributed broadcasting
The same idea extends to broadcasting: calling broadcast() instead of send() shares delivery across users, who automatically forward messages to their neighbors.
TURN and connection recovery
Even users on networks that block direct P2P can join through a blind WebSocket relay, which forwards encrypted binary data it can't read. These users can participate in multi-hop routing and bridge isolated groups.
The P2P stack mixes and matches all available paths and switches to more direct connections as they become available.
Simple p2p streams
Nostr4j now offers a simple abstraction over a subset of its P2P stack, built on standard Java input and output streams, so apps can use it just like regular Java I/O without handling any networking details.
Local event caching
Beyond networking, the release adds more options for local event caching. You can keep events in memory, persist them through a virtual filesystem, or supply your own event store. NostrCacheRelay then exposes those events to your NostrPool as a regular relay, so you query cached data the same way as you would do for online relays.
Relay pool management
Managing large relay lists is also easier with connection budgets, priorities, and hints. Your app can adjust priorities as its needs change, and Nostr4j keeps the relevant relays connected without opening the entire list at once. Hints give priority to relays likely to hold the events you're fetching or subscribing to, for as long as the operation is active. Meanwhile, a background queue makes a best-effort attempt to deliver events to every inactive registered relay.
More
- NIP-42 authentication for relays that require it.
- NIP-59 gift wrapping for private messages.
- WebLN support and QR pairing for compatible Nostr Wallet Connect wallets.
- Extended Blossom support, with mirroring and media optimization
- ln address support in nwc pairing url
—
I built this mainly for my own projects, but I hope others will find it useful too.
Noted....thanks for sharing for the comunity
I have two questions:
Why not? It is one of the best languages for this.
Nostr4j p2p stack is older than FIPS and it is to create p2p apps, not to proxy existing traffic. Nostr4j's p2p was also built specifically to sustain games' netcode (as part of ngengine work) so it has some features and optimizations needed for that,
eg.
- it prefers lower latency direct connection (as opposed to the mesh route) when possible, since in most use cases peer count will be low enough to connect to all of them directly
- has little overhead on both the rtc route and the TURN fallback
- lets you build unreliable chanels
- runs on the browser and natively on every other platform
It is my own take to p2p networking