Relay Management
Dynamically manage Nostr relays in your Portal instance.
Overview
Relays are Nostr servers that store and forward messages. Portal connects to multiple relays for redundancy and better message delivery.
Adding Relays
const relayUrl = 'wss://relay.damus.io';
const addedRelay = await client.addRelay(relayUrl);
console.log('Added relay:', addedRelay);
Removing Relays
const relayUrl = 'wss://relay.damus.io';
const removedRelay = await client.removeRelay(relayUrl);
console.log('Removed relay:', removedRelay);
Popular Relays
Here are some reliable public relays:
wss://relay.damus.io- Popular, well-maintainedwss://relay.snort.social- Fast and reliablewss://nos.lol- Good for paymentswss://relay.nostr.band- Large relay networkwss://nostr.wine- Paid relay (more reliable)
Best Practices
- Use 3-5 relays: Balance between redundancy and bandwidth
- Geographic diversity: Choose relays in different locations
- Mix free and paid: Paid relays often have better uptime
- Monitor connectivity: Remove relays that are consistently offline
- User preferences: Respect user's preferred relays from handshake
Relay Configuration Example
class RelayManager {
private client: PortalSDK;
private activeRelays = new Set<string>();
async setupDefaultRelays() {
const defaultRelays = [
'wss://relay.damus.io',
'wss://relay.snort.social',
'wss://nos.lol'
];
for (const relay of defaultRelays) {
try {
await this.client.addRelay(relay);
this.activeRelays.add(relay);
console.log('✅ Connected to', relay);
} catch (error) {
console.error('❌ Failed to connect to', relay);
}
}
}
async addUserRelays(preferredRelays: string[]) {
// Add user's preferred relays from handshake
for (const relay of preferredRelays) {
if (!this.activeRelays.has(relay)) {
try {
await this.client.addRelay(relay);
this.activeRelays.add(relay);
} catch (error) {
console.error('Failed to add user relay:', relay);
}
}
}
}
}
Next: API Reference