Skip to content

Commit

Permalink
Merge pull request #84 from tiagosiebler/dependfix
Browse files Browse the repository at this point in the history
chore(v1.4.12): bump dependencies, add example for #83.
  • Loading branch information
tiagosiebler authored Nov 25, 2024
2 parents fa2833f + 1226a01 commit 0c946fa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
66 changes: 66 additions & 0 deletions examples/ws-private-handle-auth-fail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// If you cloned the repo and are using typescript, you can import from src directly:
import { WebsocketClient } from '../src';

/**
*
* This simple example demonstrates one way to handle failed authentication, stopping the
* websocket client from running into a reconnect-loop when authentication fails (e.g. bad API keys).
*
* However, keep in mind it might be safer to make a test REST API call (e.g. fetch account balance) before
* trying to make a private WS connection to the account.
*
*/

const wsClient = new WebsocketClient({
accounts: [
// For private topics, include one or more accounts in an array. Otherwise only public topics will work
{
apiKey: 'INCORRECT_API_KEY',
apiSecret: 'INCORRECT_API_SECRET',
apiPass: 'INCORRECT_API_PASSPHRASE',
},
],
});

// Raw data will arrive on the 'update' event
wsClient.on('update', (data) => {
// console.log('ws update (raw data received)', JSON.stringify(data, null, 2));
console.log('ws update (raw data received)', JSON.stringify(data));
});

wsClient.on('open', (data) => {
console.log('connection opened open:', data.wsKey);
});

// Replies (e.g. authenticating or subscribing to channels) will arrive on the 'response' event
wsClient.on('response', (data) => {
// console.log('ws response: ', JSON.stringify(data, null, 2));
console.log('ws response: ', JSON.stringify(data));
});

wsClient.on('reconnect', ({ wsKey }) => {
console.log('ws automatically reconnecting.... ', wsKey);
});
wsClient.on('reconnected', (data) => {
console.log('ws has reconnected ', data?.wsKey);
});

wsClient.on('error', (data) => {
console.error('ws exception: ', data);

const INVALID_API_KEY_ERROR = '60005';
if (data.event === 'error' && data.code === INVALID_API_KEY_ERROR) {
console.error(`Detected auth failure - closing websocket`);
wsClient.close(data.wsKey);
}
});

// Optional, connect before subscribing:
wsClient.connectPrivate();

// This is optional though. The wsclient will automatically open and subscribe if the connection doesn't exist yet.

// Subscribe one event at a time:
wsClient.subscribe({
channel: 'account',
});
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "okx-api",
"version": "1.4.11",
"version": "1.4.12",
"description": "Complete & robust Node.js SDK for OKX's REST APIs and WebSockets, with TypeScript & end-to-end tests",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down

0 comments on commit 0c946fa

Please sign in to comment.