Boros WebSocket
This guide explains how to directly connect to Boros's WebSocket service using Socket.IO client.
Basic Usageβ
Here's a complete example of how to connect to and use the WebSocket:
import { io } from 'socket.io-client';
// Initialize the socket connection
const socket = io('wss://secrettune.io/pendle-dapp-v3', {
path: '/socket.io',
reconnectionAttempts: 5,
transports: ['websocket']
});
Connection Eventsβ
Handling Connectionβ
socket.on('connect', () => {
console.log('Connected to WebSocket server');
});
socket.on('disconnect', () => {
console.log('Disconnected from WebSocket server');
});
socket.on('connect_error', (error) => {
console.error('Connection error:', error);
});
Subscribing to Channelsβ
To receive updates, you need to:
- Subscribe to a channel
- Listen for updates on that channel
// Subscribe to a channel
socket.emit('subscribe', 'orderbook:MARKET_ID:full');
// Listen for updates
socket.on('orderbook:MARKET_ID:full:update', (data) => {
console.log('Received orderbook update:', data);
});
Cleanupβ
Always clean up your WebSocket connections when they're no longer needed:
function cleanup() {
// Unsubscribe from channels
socket.emit('unsubscribe', 'orderbook:MARKET_ID:full');
// Remove listeners
socket.off('orderbook:MARKET_ID:full:update');
// Disconnect
socket.disconnect();
}
Channel Typesβ
Common channel patterns:
orderbook:MARKET_ID:full
- Full orderbook updatesorderbook:MARKET_ID:update
- Incremental orderbook updatesmarket-trade:MARKET_ID
- Market trade updatesstatistics:MARKET_ID
- Market statistics updates
Replace MARKET_ID
with your specific market identifier.
Best Practicesβ
-
Connection Management
- Always handle connection errors
- Implement reconnection logic if needed
- Clean up connections when no longer needed
-
Event Handling
- Subscribe to channels after connection is established
- Remove listeners before disconnecting
- Handle potential errors in data processing
-
Resource Management
- Unsubscribe from channels you no longer need
- Don't create multiple connections unnecessarily
- Clean up resources when your application closes
Example Implementationβ
Here's a complete example putting it all together:
import { io } from 'socket.io-client';
class PendleWebSocket {
private socket: any;
constructor() {
this.socket = io('wss://secrettune.io/pendle-dapp-v3', {
path: '/socket.io',
reconnectionAttempts: 5,
transports: ['websocket']
});
this.setupEventHandlers();
}
private setupEventHandlers() {
this.socket.on('connect', () => {
console.log('Connected to WebSocket server');
});
this.socket.on('disconnect', () => {
console.log('Disconnected from WebSocket server');
});
this.socket.on('connect_error', (error) => {
console.error('Connection error:', error);
});
}
public subscribeToMarket(marketId: string) {
const channel = `orderbook:$\{marketId\}:full`;
this.socket.emit('subscribe', channel);
this.socket.on(`${channel}:update`, (data) => {
console.log(`Received update for $\{marketId\}:`, data);
});
}
public unsubscribeFromMarket(marketId: string) {
const channel = `orderbook:$\{marketId\}:full`;
this.socket.emit('unsubscribe', channel);
this.socket.off(`${channel}:update`);
}
public disconnect() {
this.socket.disconnect();
}
}
// Usage example:
const ws = new PendleWebSocket();
ws.subscribeToMarket('YOUR_MARKET_ID');
// Clean up when done
// ws.unsubscribeFromMarket('YOUR_MARKET_ID');
// ws.disconnect();
Error Handlingβ
Always implement proper error handling:
socket.on('connect_error', (error) => {
console.error('Connection failed:', error);
// Implement your error handling logic
});
socket.on('error', (error) => {
console.error('Socket error:', error);
// Implement your error handling logic
});