Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add connection events and subscription to multiple addresses, t… #67

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

yoxira
Copy link
Member

@yoxira yoxira commented Dec 15, 2024

feat: add connection events and subscription to multiple addresses, types

Added

  • Reconnecting behavior on disconnection and on connection errors, controlled by options.maxTries and options.reconnectionDelay

  • onConnection(connectedNode: string) and onReconnectoin(connectedNode: string) methods

  • Error on disconnection with reason code and message:

    socket.catch((error: unknown) => {
      if (error instanceof AdamantWsConnectionError) {
        console.log(error.reason, error.message)
      }
    })
  • admAddresses, types and assetChatTypes options to subscribe to the corresponding transactions

  • connect() and disconnect() methods

simple test script

const nodes = [
  'https://endless.adamant.im',
  'https://clown.adamant.im',
  'http://23.226.231.225:36666',
  'http://88.198.156.44:36666',
  'https://lake.adamant.im',
];

const admAddresses = ['U8084717991279447871'];

const socket = new WebSocketClient({
  // try out adding `types` or `assetChatTypes`! 
  admAddresses,
});

// socket.onReconnection(node => console.log('successfully reconnected'))

socket.onConnection(node => {
  console.log(`We connected to ${node}...`);
  console.log('...to disconnect from it');

  // disconnect to test reconnection
  socket.connection.disconnect();

  // // Uncomment to block following reconnections:
  // socket.nodes = [{wsType: 'ws', ip: '127.0.0.1'}];
});

socket.catch(err => {
  if (err.name === 'AdamantWsConnectionError') {
   if (err.reason === 'connection_error' || err.reason === 'disconnection') {
      console.log('We gave up on reconnecting! Reason: ', err.reason);
    }
  }
});

// We can use socket.reviseConnection(nodes) instead but then we need manually check the nodes
new AdamantApi({
  nodes,
  socket,
});

@yoxira yoxira requested a review from bludnic December 15, 2024 20:11
@yoxira
Copy link
Member Author

yoxira commented Dec 15, 2024

Updated documentation:

WebSocket Connections
https://github.com/Adamant-im/adamant-api-jsclient/wiki/WebSocket-Connections

@adamant-al adamant-al self-requested a review December 18, 2024 11:51
@yoxira yoxira requested a review from bludnic December 19, 2024 16:54
@bludnic
Copy link
Member

bludnic commented Dec 23, 2024

After pnpm install the pnpm-lock.yaml changed.

image

Can you upgrade to the latest version and commit the pnpm-lock.yaml? @yoxira

image

src/helpers/wsClient/wsClient.ts Outdated Show resolved Hide resolved
src/helpers/wsClient/wsClient.ts Show resolved Hide resolved
@yoxira yoxira requested a review from bludnic December 24, 2024 06:51
@adamant-al
Copy link
Member

@bludnic
Review

@yoxira
What's min ADM node version works with this?
If it's higher than 0.8.3, we better filter nodes in health check?

@bludnic
Copy link
Member

bludnic commented Jan 8, 2025

Firstly, we can prioritize WS connections for nodes with higher versions.

In the case a node with old version (<0.8.4) is chosen for WS subscription:

  1. If multiple ADM addresses are provided for subscription (options.admAddresses), only the first one will be selected, and a warning will be emitted:
    The [NODE:PORT] does not support subscription for multiple addresses. Only the U123456 address will be watched.

  2. Other options, such as filtering by options.types and options.assetChatTypes will be ignored. A warning should also be shown, indicating that these options have no effect.

@adamant-al
Copy link
Member

Firstly, we can prioritize WS connections for nodes with higher versions.

In the case a node with old version (<0.8.4) is chosen for WS subscription:

  1. If multiple ADM addresses are provided for subscription (options.admAddresses), only the first one will be selected, and a warning will be emitted:
    The [NODE:PORT] does not support subscription for multiple addresses. Only the U123456 address will be watched.
  2. Other options, such as filtering by options.types and options.assetChatTypes will be ignored. A warning should also be shown, indicating that these options have no effect.

I suppose ignore nodes with old version (<0.8.4).
Soon users update most nodes, and we'll not support legacy.

@bludnic
Copy link
Member

bludnic commented Jan 8, 2025

@adamant-al I agree. Let's do as you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants