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

v4.0.0: WebSocket API, WebsocketClient upgrades, decommission deprecated v1-v3 API groups #398

Merged
merged 69 commits into from
Mar 4, 2025

Conversation

tiagosiebler
Copy link
Owner

@tiagosiebler tiagosiebler commented Dec 13, 2024

Released

The release is now live, as of v4.0.0 on npm. Like/retweet/follow the announcement on X:
https://x.com/quantSDKs/status/1896897683526222318

Public Beta

The public beta release is now available on npm.

npm install bybit-api@beta

Summary

This major release brings major upgrades to the Node.js, TypeScript & JavaScript SDK for Bybit's REST APIs and WebSockets.

Key points, with further details below:

Major Changes

Auth/Performance (REST & WS)
  • Use the Web Crypto API for request sign/auth by default.
    • This is marginally slower than node's native crypto module, but provides better compatibility with browser environments.
    • See Web Crypto API #321 for context.
  • Add customSignMessageFn() parameter to WebsocketClient and RestClientV5.
    • If you're latency sensitive, use this to inject a custom sign method to use something faster than the Web Crypto API.
    • See the examples/fasterHmacSign.ts example for a demonstration on using the faster node crypto createHmac function instead (same behaviour as before this upgrade).
WebsocketClient
  • The market setting in the constructor is now optional.
    • Unless market is specified, it will use v5 by default.
  • Introduced sendWSAPIRequest(WS_KEY_MAP.v5PrivateTrade, command, parameters) method to send WS API requests. See examples/ws-api-promises.ts for examples.

Breaking Changes

REST Clients
  • Removed decommissioned V1-V3 REST clients.
    • Except for one endpoint, these have been decommissioned for a while (or are imminently going offline).
    • Anyone still using any should upgrade to V5, as they are at risk of going offline at any moment.
WebsocketClient
  • Renamed "error" emitted event to "exception".
  • Removed subscribeV3 and unsubscribeV3 methods. The V3 (& older) WebSockets have been deprecated for a while now.
  • Consolidated the internal DefaultLogger methods.
    • Into 3 key levels: trace | info | error.
    • Similar to the silly level before this release, the trace level is disabled/silent by default.
  • All connect calls now return a promise resolving to WSConnectedResult, instead of the raw WebSocket.
    • This is a simple object containing the WsKey (which can be used to retrieve the raw WebSocket, if needed).
  • The "req_id" property shown in ws responses now:
    • For responses to subscribe/unsubscribe requests, has not changed.
    • For responses to all other ws requests/commands, it now has an incremental ID value used to uniquely identify individual requests.
    • This was previously a comma-separated list of topics for that event.
  • Deprecated fetchTimeOffsetBeforeAuth.
  • Minimal support for using await when subscribing to topics:
    • To reduce the risk of unexpected unhandled promise rejections, this feature is disabled by default. See promiseSubscribeRequests in the WS client configuration.
    • This was originally added in a contributor PR (#218 Returning a promise when subscribing to topic(s) #256), but designing a robust implementation around this introduces quite a bit of complexity.
    • When subscribing to multiple topics in one call, these are automatically batched into smaller requests (to stay within the max-topics-per-request limit).
    • The only way to track which "request" resolved/rejected successfully, requires including a "request id" with the subscribe request, since the confirmation from Bybit does not include a list of successfully subscribed topics.
    • Multiple subscribe requests = multiple promises (array of promises) returned by the subscribeV5 call.
    • The new implementation is an improvement in tracking this reliably, but if you see issues with complex case scenarios (mixing public & private topics in one request), I recommend that you design your own implementation using the "response" and "exception" events.
    • The current design will only work if the connection is opened before making the subscribe call, due to the queued subscription & request batching processes. It's therefore recommended that you do that first, before expecting to await subscribe:
  try {
    console.log('connecting private & public');

    await wsClient.assertIsConnected(WS_KEY_MAP.v5LinearPublic);
    await wsClient.assertIsConnected(WS_KEY_MAP.v5Private);
    await wsClient.assertIsAuthenticated(WS_KEY_MAP.v5Private);

    console.log('should be connected now. subscribing to topics');
    const subRequest = wsClient.subscribeV5(
      ['order', 'wallet', 'position', 'execution'],
      'linear',
    );

    const results = await Promise.all(subRequest);

    console.log(
      'should be subscribed',
      JSON.stringify(results, null, 2),
    );
  } catch (e) {
    console.error('exception subscribing to topics: ', e);
  }

Additional Information

Self todo notes

  • Readme updates
  • Check any internal logic that assumed req_id had topics in it.
  • Check subscription req works the same as before
  • Check subscriptions responses are the same behaviour
  • Check public data arrives in same format as before
  • Check private data arrives in same format as before
  • Stress testing public ws
  • Stress testing private data
  • Check exceptions are emitted in the same format as before
  • Test on-connect promise assertion.
  • Test on-subscribe promise assertion subscribeV5 unsubscribeV5 promise not resolved #399
  • Stress test bulk-subscribe public ws
  • Stress test error handling
  • Stress test topic deduping

… add promisified WS workflows, feat(): add WS API integration
@tiagosiebler tiagosiebler reopened this Jan 16, 2025
@tiagosiebler tiagosiebler added enhancement New feature or request WIP Work in progress labels Jan 16, 2025
@tiagosiebler tiagosiebler mentioned this pull request Jan 22, 2025
@tiagosiebler
Copy link
Owner Author

Published a small beta release:

@tiagosiebler
Copy link
Owner Author

Published a small beta release:

Unless any issues are discovered, this is most likely the final beta release before the major release goes live.

@tiagosiebler
Copy link
Owner Author

Published a small beta release:

  • v4.0.0-beta.9: fix() batch order ws api handling

Unless any issues are discovered, this is most likely the final beta release before the major release goes live.

@tiagosiebler tiagosiebler marked this pull request as ready for review March 4, 2025 10:37
Copy link
Owner Author

@tiagosiebler tiagosiebler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final self-review complete. Awaiting CI/CD checks.

@tiagosiebler tiagosiebler self-assigned this Mar 4, 2025
@tiagosiebler tiagosiebler merged commit ca9a7d7 into master Mar 4, 2025
3 checks passed
@tiagosiebler tiagosiebler deleted the wsapi branch March 4, 2025 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Outdated documentation
2 participants