@coinex-org/websocket
v0.0.0
Published
A lightweight, type-safe TypeScript WebSocket client for the official CoinEx API v2, providing real-time market and user data.
Downloads
29
Maintainers
Readme
@coinex-org/websocket
A lightweight, type-safe TypeScript client for the official CoinEx WebSocket API v2.
Features
- Type-Safe: Fully typed subscriptions and events for a better developer experience.
- Dual Clients: Separate, dedicated WebSocket clients for Spot and Futures markets.
- Event-Driven: Use
EventEmitterto handle WebSocket events likeopen,close, anderror. - Auto-Authentication: Handles authentication automatically for private channels.
- Auto-Reconnect: Automatically reconnects if the connection is lost.
Installation
npm install @coinex-org/websocketpnpm
pnpm install @coinex-org/websocketyarn
yarn add @coinex-org/websocketUsage
First, import and initialize the CoinExWebSocket client with your API Key and Secret Key.
import { CoinExWebSocket } from '@coinex-org/websocket';
const client = new CoinExWebSocket({
apiKey: 'YOUR_API_KEY',
secretKey: 'YOUR_SECRET_KEY',
});
client.on('open', (type) => {
console.log(`WebSocket connection opened for ${type}`);
// Subscribe to public Spot market data
if (type === 'spot') {
console.log('Subscribing to BTCUSDT ticker...');
client.spot.ticker.state.subscribe(
{ market_list: ['BTCUSDT'] },
(update) => {
console.log('Received spot ticker update:', update);
}
);
}
// Subscribe to private Futures data
if (type === 'futures') {
console.log('Subscribing to futures balance...');
client.futures.balance.subscribe({ ccy_list: [] }, (update) => {
console.log('Received futures balance update:', update);
});
}
});
client.on('error', (error, type) => {
console.error(`Error on ${type} WebSocket:`, error);
});
client.on('close', (type) => {
console.log(`WebSocket connection closed for ${type}`);
});Documentation
For all configuration options, please see the API docs.
API Reference
CoinExWebSocket
Events
on('open', (type: 'spot' | 'futures') => void): Emitted when a WebSocket connection is successfully opened and authenticated.on('close', (type: 'spot' | 'futures') => void): Emitted when a WebSocket connection is closed.on('error', (error: Error, type: 'spot' | 'futures') => void): Emitted when a WebSocket error occurs.on('message', (message: any, type: 'spot' | 'futures') => void): Emitted when any message is received.
Methods
connect(): Manually initiate the WebSocket connections.close(): Manually close the WebSocket connections.ping(): Ping the server to check connectivity.getServerTime(): Get the current server time.
Spot Subscriptions (client.spot)
- Balance:
subscribe,unsubscribe - Executions:
subscribe,unsubscribe - Orders:
subscribe,unsubscribe- Stop Orders:
stop.subscribe,stop.unsubscribe
- Stop Orders:
- Ticker:
- BBO:
bbo.subscribe,bbo.unsubscribe - Deals:
deals.subscribe,deals.unsubscribe - Depth:
depth.subscribe,depth.unsubscribe - Index:
index.subscribe,index.unsubscribe - State:
state.subscribe,state.unsubscribe
- BBO:
Futures Subscriptions (client.futures)
- Balance:
subscribe,unsubscribe - Executions:
subscribe,unsubscribe - Orders:
subscribe,unsubscribe- Stop Orders:
stop.subscribe,stop.unsubscribe
- Stop Orders:
- Position:
subscribe,unsubscribe - Ticker:
- BBO:
bbo.subscribe,bbo.unsubscribe - Deals:
deals.subscribe,deals.unsubscribe - Depth:
depth.subscribe,depth.unsubscribe - Index:
index.subscribe,index.unsubscribe - State:
state.subscribe,state.unsubscribe
- BBO:
License
MIT © Shahrad Elahi and contributors.
