diff --git a/package.json b/package.json index d35a628..8b3b820 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "lib/index.js", "license": "Apache-2.0", "dependencies": { - "@drift-labs/sdk": "2.46.0-beta.1", + "@drift-labs/sdk": "2.47.0-beta.0", "@opentelemetry/api": "^1.1.0", "@opentelemetry/auto-instrumentations-node": "^0.31.1", "@opentelemetry/exporter-prometheus": "^0.31.0", diff --git a/src/wsConnectionManager.ts b/src/wsConnectionManager.ts index 8b7283f..cb383aa 100644 --- a/src/wsConnectionManager.ts +++ b/src/wsConnectionManager.ts @@ -28,8 +28,15 @@ const wss = new WebSocketServer({ server, path: '/ws' }); const REDIS_HOST = process.env.REDIS_HOST || 'localhost'; const REDIS_PORT = process.env.REDIS_PORT || '6379'; const WS_PORT = process.env.WS_PORT || '3000'; + +console.log(`WS LISTENER PORT : ${WS_PORT}`); + const REDIS_PASSWORD = process.env.REDIS_PASSWORD; +const safeGetRawChannelFromMessage = (message: any): string => { + return message?.channel; +}; + const getRedisChannelFromMessage = (message: any): string => { const channel = message.channel; const marketName = message.market?.toUpperCase(); @@ -119,7 +126,17 @@ async function main() { try { redisChannel = getRedisChannelFromMessage(parsedMessage); } catch (error) { - ws.send(JSON.stringify({ error: error.message })); + const requestChannel = safeGetRawChannelFromMessage(parsedMessage); + if (requestChannel) { + ws.send( + JSON.stringify({ + channel: requestChannel, + error: error.message, + }) + ); + } else { + ws.close(1003, JSON.stringify({ error: error.message })); + } return; } @@ -149,12 +166,17 @@ async function main() { // Fetch and send last message if (redisChannel.includes('orderbook')) { + const lastUpdateChannel = `last_update_${redisChannel}`; const lastMessage = await lastMessageRetriever.client.get( - `last_update_${redisChannel}` + lastUpdateChannel ); + if (lastMessage !== null) { ws.send( - JSON.stringify({ channel: redisChannel, data: lastMessage }) + JSON.stringify({ + channel: lastUpdateChannel, + data: lastMessage, + }) ); } } @@ -165,7 +187,17 @@ async function main() { try { redisChannel = getRedisChannelFromMessage(parsedMessage); } catch (error) { - ws.send(JSON.stringify({ error: error.message })); + const requestChannel = safeGetRawChannelFromMessage(parsedMessage); + if (requestChannel) { + ws.send( + JSON.stringify({ + channel: requestChannel, + error: error.message, + }) + ); + } else { + ws.close(1003, JSON.stringify({ error: error.message })); + } return; } const subscribers = channelSubscribers.get(redisChannel); diff --git a/yarn.lock b/yarn.lock index 3fa51b3..ef5f335 100644 --- a/yarn.lock +++ b/yarn.lock @@ -80,10 +80,10 @@ enabled "2.0.x" kuler "^2.0.0" -"@drift-labs/sdk@2.46.0-beta.1": - version "2.46.0-beta.1" - resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.46.0-beta.1.tgz#0fea7be51a07180479418ee559fa916ee306db9b" - integrity sha512-r5edKj5UlZPn6ChWab9vlBWVeHQayszF/RdLr/IU4+iAYcT0VYSbkI53WmB86z6ox07uBK9VGvtPQlfKtrVh0A== +"@drift-labs/sdk@2.47.0-beta.0": + version "2.47.0-beta.0" + resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.47.0-beta.0.tgz#2c29d46aa1b9aaacc716bf980117e8060a92ae8d" + integrity sha512-6CGjwu1OzeuI9VG7CrZzRc4kbdfXxcLSuq33se0WukPbK9yNgprCpm4S49Xk7ubx+019DbeTAwxzajXo+V6DgQ== dependencies: "@coral-xyz/anchor" "0.28.1-beta.2" "@ellipsis-labs/phoenix-sdk" "^1.4.2"