|
475 | 475 |
|
476 | 476 | <script src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/protobuf.min.js" ></script> |
477 | 477 | <script type="module"> |
478 | | - import { createLightNode, waitForRemotePeer, Protocols } from 'https://unpkg.com/@waku/[email protected]/bundle/index.js'; |
| 478 | + import { createLightNode, Protocols } from 'https://unpkg.com/@waku/[email protected]/bundle/index.js'; |
479 | 479 | import { MessageChannel, MessageChannelEvent, encodeMessage, decodeMessage } from 'https://unpkg.com/@waku/[email protected]/bundle/index.js'; |
480 | 480 |
|
481 | 481 | // Define protobuf schema |
|
503 | 503 | this.messageChannel = null; // SDS Message Channel |
504 | 504 | this.displayedMessages = new Set(); // Track displayed SDS message IDs to prevent dupes |
505 | 505 | this.displayedContent = new Set(); // Track displayed content (timestamp+text) to prevent content dupes |
| 506 | + this.isConnectedToStore = false; // Track store node connection |
506 | 507 |
|
507 | 508 | this.initProtobuf(); |
508 | 509 | this.initElements(); |
|
583 | 584 |
|
584 | 585 | async loadMessageHistory() { |
585 | 586 | try { |
| 587 | + // Only proceed if we're connected to a store node |
| 588 | + if (!this.isConnectedToStore) { |
| 589 | + console.log('Not connected to store node, skipping message history loading'); |
| 590 | + return; |
| 591 | + } |
| 592 | + |
586 | 593 | console.log('Starting progressive message history loading...'); |
587 | 594 |
|
588 | 595 | const now = new Date(); |
|
873 | 880 |
|
874 | 881 | // Create Waku node |
875 | 882 | this.node = await createLightNode({ defaultBootstrap: true }); |
| 883 | + |
| 884 | + // Listen for connection events using Waku event system |
| 885 | + this.node.events.addEventListener("waku:connection", (event) => { |
| 886 | + console.log('Waku connection event:', event.detail); // true if connected, false if disconnected |
| 887 | + this.checkStoreConnection(); |
| 888 | + }); |
| 889 | + |
876 | 890 | await this.node.start(); |
877 | 891 |
|
878 | 892 | // Dial the specific node |
|
886 | 900 |
|
887 | 901 | this.updateStatus('Waiting for peers...', 'connecting'); |
888 | 902 |
|
889 | | - // Wait for connection to remote peers |
890 | | - await waitForRemotePeer(this.node, [Protocols.LightPush, Protocols.Filter]); |
891 | | - |
892 | 903 | // Create encoder and decoder |
893 | 904 | this.encoder = this.node.createEncoder({ contentTopic: this.contentTopic }); |
894 | 905 | this.decoder = this.node.createDecoder({ contentTopic: this.contentTopic }); |
895 | 906 |
|
896 | | - // Subscribe to messages |
| 907 | + // Subscribe to messages (don't wait for specific protocol peers) |
897 | 908 | await this.node.filter.subscribe([this.decoder], (message) => { |
898 | 909 | this.handleIncomingWakuMessage(message); |
899 | 910 | }); |
900 | 911 |
|
901 | | - this.updateStatus('Loading history...', 'connecting'); |
902 | | - |
903 | | - // Query store for messages from the past 24 hours |
904 | | - await this.loadMessageHistory(); |
905 | | - |
906 | | - this.updateStatus('Connected', 'connected'); |
| 912 | + // Enable sending immediately |
907 | 913 | this.messageInput.disabled = false; |
908 | 914 | this.sendButton.disabled = false; |
909 | 915 |
|
| 916 | + this.updateStatus('Connected', 'connected'); |
| 917 | + |
910 | 918 | // Start monitoring node connections |
911 | 919 | this.startNodeCountMonitoring(); |
| 920 | + this.startStoreMonitoring(); |
912 | 921 |
|
913 | | - this.addSystemMessage('Connected to Waku network with SDS reliability! You can now chat.'); |
| 922 | + this.addSystemMessage('Connected to Waku network! Waiting for store nodes to load history...'); |
914 | 923 |
|
915 | 924 | } catch (error) { |
916 | 925 | console.error('Failed to initialize Waku:', error); |
|
919 | 928 | } |
920 | 929 | } |
921 | 930 |
|
| 931 | + async checkStoreConnection() { |
| 932 | + if (!this.node) return; |
| 933 | + |
| 934 | + try { |
| 935 | + // Get all connected peers with their protocol support |
| 936 | + const connectedPeers = await this.node.getConnectedPeers(); |
| 937 | + |
| 938 | + // Find peers that support the store protocol |
| 939 | + const storePeers = connectedPeers.filter(peerInfo => |
| 940 | + peerInfo.protocols && peerInfo.protocols.includes('/vac/waku/store/2.0.0-beta4') |
| 941 | + ); |
| 942 | + |
| 943 | + const hasStore = storePeers.length > 0; |
| 944 | + |
| 945 | + console.log(`Connected peers: ${connectedPeers.length}, Store peers: ${storePeers.length}`); |
| 946 | + if (storePeers.length > 0) { |
| 947 | + console.log('Store peers:', storePeers.map(p => p.id.toString())); |
| 948 | + } |
| 949 | + |
| 950 | + // Track connection state changes |
| 951 | + const wasConnectedToStore = this.isConnectedToStore; |
| 952 | + this.isConnectedToStore = hasStore; |
| 953 | + |
| 954 | + // Start store queries when we first connect to a store node |
| 955 | + if (!wasConnectedToStore && hasStore) { |
| 956 | + console.log('Connected to store node, starting message history loading...'); |
| 957 | + this.addSystemMessage('Store node connected, loading message history...'); |
| 958 | + await this.loadMessageHistory(); |
| 959 | + } else if (wasConnectedToStore && !hasStore) { |
| 960 | + console.log('Lost connection to store nodes'); |
| 961 | + this.addSystemMessage('Lost connection to store nodes - history loading unavailable'); |
| 962 | + } |
| 963 | + |
| 964 | + } catch (error) { |
| 965 | + console.error('Failed to check store connection:', error); |
| 966 | + } |
| 967 | + } |
| 968 | + |
| 969 | + startStoreMonitoring() { |
| 970 | + // Check store connection every 5 seconds |
| 971 | + setInterval(() => { |
| 972 | + this.checkStoreConnection(); |
| 973 | + }, 5000); |
| 974 | + } |
| 975 | + |
922 | 976 | startNodeCountMonitoring() { |
923 | 977 | // Update node count immediately and then every 5 seconds |
924 | 978 | this.updateNodeCountFromWaku(); |
|
0 commit comments