feat: Enhance peer subscription and logging in Nostr chat
- Improve the subscription process for peers by adding detailed logging for subscription attempts and results. - Update the ChatComponent to provide a summary of successful and failed subscriptions. - Enhance the useNostrChat composable with additional logging for subscription filters and incoming message handling. - Ensure better visibility into connection state changes and subscription readiness.
This commit is contained in:
parent
d48cbbeec0
commit
a0ae70670d
2 changed files with 68 additions and 18 deletions
|
|
@ -538,16 +538,30 @@ const subscribeToAllPeers = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Subscribing to ${peers.value.length} peers for notifications`)
|
console.log(`Subscribing to ${peers.value.length} peers for notifications`)
|
||||||
|
console.log('Peers to subscribe to:', peers.value.map(p => ({ pubkey: p.pubkey, username: p.username })))
|
||||||
|
|
||||||
|
let successCount = 0
|
||||||
|
let errorCount = 0
|
||||||
|
|
||||||
for (const peer of peers.value) {
|
for (const peer of peers.value) {
|
||||||
try {
|
try {
|
||||||
|
console.log(`Attempting to subscribe to peer: ${peer.pubkey} (${peer.username})`)
|
||||||
// Subscribe to peer for notifications only (don't load full history)
|
// Subscribe to peer for notifications only (don't load full history)
|
||||||
await subscribeToPeerForNotifications(peer.pubkey)
|
const subscription = await subscribeToPeerForNotifications(peer.pubkey)
|
||||||
|
if (subscription) {
|
||||||
console.log(`Successfully subscribed to notifications for peer: ${peer.pubkey}`)
|
console.log(`Successfully subscribed to notifications for peer: ${peer.pubkey}`)
|
||||||
|
successCount++
|
||||||
|
} else {
|
||||||
|
console.warn(`Failed to create subscription for peer: ${peer.pubkey}`)
|
||||||
|
errorCount++
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(`Failed to subscribe to peer ${peer.pubkey}:`, error)
|
console.warn(`Failed to subscribe to peer ${peer.pubkey}:`, error)
|
||||||
|
errorCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Subscription summary: ${successCount} successful, ${errorCount} failed`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const refreshPeers = () => {
|
const refreshPeers = () => {
|
||||||
|
|
@ -646,8 +660,22 @@ onMounted(async () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Starting connection and peer loading...')
|
||||||
await connect()
|
await connect()
|
||||||
|
console.log('Connection established, loading peers...')
|
||||||
await loadPeers()
|
await loadPeers()
|
||||||
|
console.log('Peers loaded, checking if we should subscribe...')
|
||||||
|
|
||||||
|
// If we're connected and have peers, subscribe to them
|
||||||
|
if (isConnected.value && peers.value.length > 0) {
|
||||||
|
console.log('Connection and peers ready, subscribing to all peers for notifications')
|
||||||
|
await subscribeToAllPeers()
|
||||||
|
} else {
|
||||||
|
console.log('Not ready to subscribe yet:', {
|
||||||
|
isConnected: isConnected.value,
|
||||||
|
peerCount: peers.value.length
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
|
|
@ -656,9 +684,10 @@ onUnmounted(() => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Watch for connection state changes and subscribe to peers when connected
|
// Watch for connection state changes and subscribe to peers when connected
|
||||||
watch(isConnected, async (connected) => {
|
watch(isConnected, async (connected, prevConnected) => {
|
||||||
if (connected && peers.value.length > 0) {
|
console.log('Connection state changed:', { connected, prevConnected, peerCount: peers.value.length })
|
||||||
console.log('Connection established, subscribing to peers for notifications')
|
if (connected && peers.value.length > 0 && !prevConnected) {
|
||||||
|
console.log('Connection established and peers available, subscribing to peers for notifications')
|
||||||
await subscribeToAllPeers()
|
await subscribeToAllPeers()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -341,6 +341,9 @@ export function useNostrChat() {
|
||||||
|
|
||||||
// Subscribe to a peer for notifications only (without loading full message history)
|
// Subscribe to a peer for notifications only (without loading full message history)
|
||||||
const subscribeToPeerForNotifications = async (peerPubkey: string) => {
|
const subscribeToPeerForNotifications = async (peerPubkey: string) => {
|
||||||
|
console.log('=== SUBSCRIBE TO PEER FOR NOTIFICATIONS START ===')
|
||||||
|
console.log('Peer pubkey:', peerPubkey)
|
||||||
|
|
||||||
if (!currentUser.value) {
|
if (!currentUser.value) {
|
||||||
console.warn('No user logged in - cannot subscribe to peer notifications')
|
console.warn('No user logged in - cannot subscribe to peer notifications')
|
||||||
return null
|
return null
|
||||||
|
|
@ -362,14 +365,14 @@ export function useNostrChat() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const myPubkey = currentUser.value.pubkey
|
const myPubkey = currentUser.value.pubkey
|
||||||
|
console.log('My pubkey:', myPubkey)
|
||||||
|
|
||||||
// Subscribe to new messages only (no historical messages)
|
// Subscribe to new messages only (no historical messages)
|
||||||
const relayConfigs = getRelays()
|
const relayConfigs = getRelays()
|
||||||
console.log('Subscribing to notifications for peer:', peerPubkey, 'with my pubkey:', myPubkey)
|
console.log('Subscribing to notifications for peer:', peerPubkey, 'with my pubkey:', myPubkey)
|
||||||
|
console.log('Using relays:', relayConfigs.map(r => r.url))
|
||||||
|
|
||||||
const sub = pool.value.subscribeMany(
|
const filters = [
|
||||||
relayConfigs.map(r => r.url),
|
|
||||||
[
|
|
||||||
{
|
{
|
||||||
kinds: [4],
|
kinds: [4],
|
||||||
authors: [peerPubkey],
|
authors: [peerPubkey],
|
||||||
|
|
@ -380,10 +383,22 @@ export function useNostrChat() {
|
||||||
authors: [myPubkey],
|
authors: [myPubkey],
|
||||||
'#p': [peerPubkey]
|
'#p': [peerPubkey]
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
|
|
||||||
|
console.log('Notification subscription filters:', JSON.stringify(filters, null, 2))
|
||||||
|
|
||||||
|
const sub = pool.value.subscribeMany(
|
||||||
|
relayConfigs.map(r => r.url),
|
||||||
|
filters,
|
||||||
{
|
{
|
||||||
onevent(event) {
|
onevent(event) {
|
||||||
console.log('Received notification event:', event.id, 'author:', event.pubkey, 'for peer:', peerPubkey)
|
console.log('Received notification event:', {
|
||||||
|
id: event.id,
|
||||||
|
author: event.pubkey,
|
||||||
|
forPeer: peerPubkey,
|
||||||
|
tags: event.tags,
|
||||||
|
contentLength: event.content?.length || 0
|
||||||
|
})
|
||||||
handleIncomingMessage(event, peerPubkey)
|
handleIncomingMessage(event, peerPubkey)
|
||||||
},
|
},
|
||||||
oneose() {
|
oneose() {
|
||||||
|
|
@ -393,6 +408,7 @@ export function useNostrChat() {
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log('Successfully created notification subscription for peer:', peerPubkey)
|
console.log('Successfully created notification subscription for peer:', peerPubkey)
|
||||||
|
console.log('=== SUBSCRIBE TO PEER FOR NOTIFICATIONS END ===')
|
||||||
return sub
|
return sub
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -446,11 +462,16 @@ export function useNostrChat() {
|
||||||
|
|
||||||
// Handle incoming message
|
// Handle incoming message
|
||||||
const handleIncomingMessage = async (event: any, peerPubkey: string) => {
|
const handleIncomingMessage = async (event: any, peerPubkey: string) => {
|
||||||
|
console.log('=== HANDLE INCOMING MESSAGE START ===')
|
||||||
|
console.log('Event ID:', event.id, 'Peer:', peerPubkey)
|
||||||
|
|
||||||
if (processedMessageIds.value.has(event.id)) {
|
if (processedMessageIds.value.has(event.id)) {
|
||||||
|
console.log('Message already processed, skipping:', event.id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
processedMessageIds.value.add(event.id)
|
processedMessageIds.value.add(event.id)
|
||||||
|
console.log('Added to processed messages:', event.id)
|
||||||
|
|
||||||
console.log('Handling incoming message:', {
|
console.log('Handling incoming message:', {
|
||||||
eventId: event.id,
|
eventId: event.id,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue