*prettify + new example

This commit is contained in:
Nour Alharithi
2023-10-29 15:55:44 -07:00
parent ee5cd4e6a8
commit f3d64ff988
2 changed files with 31 additions and 16 deletions

View File

@@ -1,17 +1,24 @@
import WebSocket from 'ws'; import WebSocket from 'ws';
// const ws = new WebSocket('wss://master.dlob.drift.trade/ws');
const ws = new WebSocket('ws://localhost:3000/ws'); const ws = new WebSocket('ws://localhost:3000/ws');
import { sleep } from '../src/utils/utils';
ws.on('open', () => { ws.on('open', async () => {
console.log('Connected to the server'); console.log('Connected to the server');
ws.send(JSON.stringify({ type: 'subscribe', channel: 'SOL-PERP' })); 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' }));
await sleep(5000);
ws.send(JSON.stringify({ type: 'unsubscribe', channel: 'SOL-PERP' }));
console.log("####################");
}); });
ws.on('message', (data: WebSocket.Data) => { ws.on('message', (data: WebSocket.Data) => {
try { try {
const message = JSON.parse(data.toString()); const message = JSON.parse(data.toString());
if (message.channel === 'SOL-PERP') { console.log(`Received data from market ${message.channel}`);
console.log('Received data:', message.data); // book data is in message.data
}
} catch (e) { } catch (e) {
console.error('Invalid message:', data); console.error('Invalid message:', data);
} }

View File

@@ -43,10 +43,18 @@ async function main() {
const channel = parsedMessage.channel; const channel = parsedMessage.channel;
if (!subscribedChannels.has(channel)) { if (!subscribedChannels.has(channel)) {
console.log('Subscribing to channel', channel); console.log('Subscribing to channel', channel);
redisClient.client.subscribe(channel).then(() => { redisClient.client
.subscribe(channel)
.then(() => {
subscribedChannels.add(channel); subscribedChannels.add(channel);
}).catch(() => { })
ws.send(JSON.stringify({channel, error: `Invalid channel: ${channel}`})); .catch(() => {
ws.send(
JSON.stringify({
channel,
error: `Invalid channel: ${channel}`,
})
);
return; return;
}); });
} }