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

updated exmaple and last update bug fix #22

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions example/wsClient.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import WebSocket from 'ws';
const ws = new WebSocket('wss://master.dlob.drift.trade/ws');
const ws = new WebSocket('wss://dlob.drift.trade/ws');
import { sleep } from '../src/utils/utils';

ws.on('open', async () => {
console.log('Connected to the server');
ws.send(JSON.stringify({ type: 'subscribe', channel: 'SOL-PERP' }));
ws.send(JSON.stringify({ type: 'subscribe', channel: 'LINK-PERP' }));
ws.send(JSON.stringify({ type: 'subscribe', channel: 'INJ-PERP' }));

// Subscribe and unsubscribe to orderbook channels
ws.send(JSON.stringify({ type: 'subscribe', marketType: 'perp', channel: 'orderbook', market: 'SOL-PERP' }));
ws.send(JSON.stringify({ type: 'subscribe', marketType: 'perp', channel: 'orderbook', market: 'INJ-PERP' }));
ws.send(JSON.stringify({ type: 'subscribe', marketType: 'perp', channel: 'orderbook', market: 'BTC-PERP' }));
ws.send(JSON.stringify({ type: 'subscribe', marketType: 'spot', channel: 'orderbook', market: 'SOL' }));
await sleep(5000);

ws.send(JSON.stringify({ type: 'unsubscribe', marketType: 'perp', channel: 'orderbook', market: 'SOL-PERP' }));
console.log("####################");


// Subscribe to trades data
ws.send(JSON.stringify({ type: 'subscribe', marketType: 'perp', channel: 'trades', market: 'SOL-PERP' }));
ws.send(JSON.stringify({ type: 'subscribe', marketType: 'spot', channel: 'trades', market: 'SOL' }));
await sleep(5000);

ws.send(JSON.stringify({ type: 'unsubscribe', channel: 'SOL-PERP' }));
ws.send(JSON.stringify({ type: 'unsubscribe', marketType: 'perp', channel: 'trades', market: 'SOL-PERP' }));
console.log("####################");
});

ws.on('message', (data: WebSocket.Data) => {
try {
const message = JSON.parse(data.toString());
console.log(`Received data from market ${message.channel}`);
// book data is in message.data
console.log(`Received data from channel: ${JSON.stringify(message.channel)}`);
// book and trades data is in message.data
} catch (e) {
console.error('Invalid message:', data);
}
Expand Down
4 changes: 3 additions & 1 deletion src/wsConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ async function main() {
`last_update_${redisChannel}`
);
if (lastMessage !== null) {
ws.send(JSON.stringify({ redisChannel, data: lastMessage }));
ws.send(
JSON.stringify({ channel: redisChannel, data: lastMessage })
);
}
}
break;
Expand Down
Loading