WebSocket Overview
The WebSocket service by Alp Exchange is a real-time communication platform for traders. It offers public channels for accessing market data like prices and order book updates, as well as private (authenticated) channels for personal fund and order change notifications. This service ensures quick access to trading information, secure authentication, efficient order management, and an enhanced user experience, catering to the needs of both public and private traders. The service is available on wss://www.alp.com/ws.
import asyncio
from pprint import pprint
from alpcom_api import ws, clients, cache
from alpcom_api.dto import ws as dto
class MyHandler(ws.Handler):
def on_ticker(self, ticker: dto.Ticker):
pprint(ticker)
def on_trade(self, trade: dto.Trade):
pprint(trade)
def on_rate(self, rate: dto.Rate):
pprint(rate)
def on_diff(self, diff: dto.Diff):
pprint(diff)
def on_depth(self, depth: dto.Depth):
pprint(depth)
def on_wallet(self, wallet: dto.Wallet):
pprint(wallet)
def on_order(self, order: dto.Order):
pprint(order)
async def main():
async with ws.Client(handler=MyHandler()) as client:
await client.subscribe(ws.tps.tickers_all)
await client.receive_messages()
asyncio.get_event_loop().run_until_complete(main())
Ping
- Ping Message: The server initiates the process by sending a "ping" message, string
1to the client at regular intervals, every 20 seconds by default. - Pong Message: Upon receiving the ping message, the client responds with a "pong" message, string
2to acknowledge that it's still active and responsive. - Connection Monitoring: The server keeps track of the time since the last ping message was sent. If it doesn't receive a corresponding pong message within a set timeframe, 30 seconds by default, it interprets this as an unresponsive client.
- Connection Closure: In the event that the server doesn't receive a pong message within the specified time frame (30 seconds by default), it takes action to close the connection. This action can include terminating the connection to free up server resources and maintain system efficiency.
Topic Subscription
Message to subscribe on topics:
["subscribe", "topic1", "topic2", ...]
Message to unsubscribe:
["unsubscribe", "topic1", "topic2", ...]
The server will respond on subscription or unsubscription with next messages
["subscribe",1586857703.901509,"topic1","topic1"]
["unsubscribe",1586857703.901509,"topic1","topic1"]
In case of any error, the server will send next message:
["error",1586857703.901509,"Topic does not exist"]
Welcome
The welcome message gets sent when a connection is established. This message provides important initialization information for the client to ensure a smooth and responsive communication experience.
Message Fields
- Type (Index 0): The type of the message is set to "welcome," indicating that it's a welcome message.
- Time (Index 1): A floating-point number representing the timestamp of the message.
- PingInterval (Index 2): An integer representing the ping interval in milliseconds. This specifies how often the server sends ping messages to the client (default: 20000 ms = 20 seconds).
- PingTimeout (Index 3): An integer representing the ping timeout in milliseconds. If the server does not receive a pong response from the client within this timeout period, it may close the connection (default: 30000 ms = 30 seconds).
- ClientID (Index 4): A unique identifier (UUID) for the client, allowing the server to distinguish between different clients.
["welcome", 1694002700, 20000, 30000, "5c00d9c2-607e-4c7f-927b-b204b29e387b"]