JavaScript Example
WebSocket Integration Guide
Complete example of integrating real-time market data using native JavaScript WebSocket API.
1
Establish Connection
Create a WebSocket instance to connect to the market data server
ConnectionJavaScript
1
2
3
const socket = new WebSocket(
'wss://api.zzztech.com.tw/ws/v1/symbol/quote?wsToken=**********'
);2
Subscribe to Symbols
Send subscription requests in the open event after connection is established
SubscribeJavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// provider: US (美股), Taiwan (台股), Binance (加密貨幣)
socket.addEventListener('open', function (event) {
// 訂閱美股 AAPL
socket.send(JSON.stringify({
event: 'subscribe',
data: { symbol: 'AAPL', provider: 'US' }
}));
// 訂閱台股 2330
socket.send(JSON.stringify({
event: 'subscribe',
data: { symbol: '2330', provider: 'Taiwan' }
}));
// 訂閱比特幣(現貨)
socket.send(JSON.stringify({
event: 'subscribe',
data: { symbol: 'BTCUSDT', provider: 'Binance', market: 'Client' }
}));
});3
Receive Data
Listen to message events to receive real-time market data
ListenJavaScript
1
2
3
4
socket.addEventListener('message', function (event) {
const data = JSON.parse(event.data);
console.log('Received:', data);
});4
Unsubscribe
Send unsubscribe request to stop receiving data for specific symbols
UnsubscribeJavaScript
1
2
3
4
socket.send(JSON.stringify({
event: 'unsubscribe',
data: { symbol: 'BTCUSDT', provider: 'Binance' }
}));5
Query Subscriptions
Query the list of currently subscribed symbols
QueryJavaScript
1
2
3
socket.send(JSON.stringify({
event: 'subscriptionList'
}));Development Tips
Replace wsToken with your actual token
Provider options: US (US Stocks), Taiwan (Taiwan Stocks), Binance (Cryptocurrency)
Binance requires market specification: Client (Spot) or FutureClient (Futures)
