XRPL-Labs / xrpl-client

Connect to the XRP Ledger using WebSockets
https://www.npmjs.com/package/xrpl-client
MIT License
12 stars 8 forks source link

Connect to the first websocket that takes a connection #6

Closed mvadari closed 1 year ago

mvadari commented 1 year ago

A feature that would be really nice is trying to connect to all the websocket URLs that are passed in all at once, then maintaining the connection that connects first. The round-robin endpoint selection can still occur after this.

One usecase would be if you're passing in a bunch of URLs to nodes and you're not sure if all of the nodes are currently running. Instead of needing to wait for each and every disconnection, you can just connect to them all at once, and you'll know that if at least one of them is connected, you're good.

This is useful for the xrpl.org Explorer, since we're sometimes passing in multiple URLs into the list of endpoints, and we're not sure if all of them are correct URLs (mostly with regard to the port number or whether they have wss enabled).

If you don't want to do this as a general feature, perhaps it could be a constructor option.

WietseWind commented 1 year ago

This was part of the previous XRPL Websocket connection lib. I built but I removed it as I felt it was a waste of resources. However, with opt-in behaviour, I can see how this is useful.

I just added it (npm: 2.0.0); you can pass it as an option: tryAllNodes: true. The connect strategy will then be to setup a connection to all, query server_info and the first one to respond with actual ledgers in complete_ledgers 'wins' the race and will be your node. The other connections will be dropped. For reconnect-attempts the same strategy will be used.

    const client = new XrplClient(
      [
        "wss://xrplcluster.com",
        "wss://xrpl.link",
        "wss://s2.ripple.com",
        "ws://fh1:8080",
        "ws://fh2:8080",
      ],
      { tryAllNodes: true }
    );